Python 正则表达式2.7

Python 正则表达式2.7,python,regex,python-2.7,Python,Regex,Python 2.7,我正在修补正则表达式,我正在尝试编辑一个字符串,以便它可以执行以下操作 原文 Mr. Bob had 1.3 pounds, he didn't have much. Did he care? Joe Bloggs, Esq. thinks he does. However, he paused... But, what will you do with the .7? 实际结果 Mr. Bob had 1.3 pounds, he didn't have much. Did he car

我正在修补正则表达式,我正在尝试编辑一个字符串,以便它可以执行以下操作

原文

Mr. Bob had 1.3 pounds, he didn't have much. Did he care? Joe Bloggs, Esq. thinks he does. However, he paused... But, what will you do with the .7?
实际结果

Mr. 
Bob had 1.3 pounds, he didn't have much. 
Did he care? 
Joe Bloggs, Esq. thinks he does. 
However, he paused... But, what will you do with the .7?
Mr. Bob had 1.3 pounds, he didn't have much.
Did he care?
Joe Bloggs, Esq. thinks he does.
However, he paused...
But, what will you do with the .7?
预期结果

Mr. 
Bob had 1.3 pounds, he didn't have much. 
Did he care? 
Joe Bloggs, Esq. thinks he does. 
However, he paused... But, what will you do with the .7?
Mr. Bob had 1.3 pounds, he didn't have much.
Did he care?
Joe Bloggs, Esq. thinks he does.
However, he paused...
But, what will you do with the .7?
到目前为止,我的声明是

print re.sub(r'(?<!..\.|.Mr|Esq|Mrs|.Ms)[.?]\s+', '\\g<0>\n', s)
print re.sub(r'(?\n',s)

您的正则表达式有
.Mr
,这要求
Mr
前面有另一个字符。但是您的
Mr
显示在字符串的开头,因此前面没有字符

使用内置的
re
模块,我看不到任何明显的解决方法。它要求lookbehinds具有固定的长度,因此没有办法对
Esq
Mr
都有一个lookbehind,前面没有任何内容。但是,
regex
库没有这个限制,因此您可以使用这个library,使用更简单的正则表达式启动:

>>> print regex.sub(r'(?<!\.|Mr|Esq|Mrs|Ms)[.?]\s+', '\\g<0>\n', s)
Mr. Bob had 1.3 pounds, he didn't have much. 
Did he care? 
Joe Bloggs, Esq. thinks he does. 
However, he paused... But, what will you do with the .7?
打印正则表达式sub(r'(?\n',s) 鲍勃先生有1.3磅,他没有多少。 他在乎吗? 乔·布洛格斯,Esq。认为他是。 然而,他停顿了一下……但是,你们将如何处理这个问题呢?
(编辑:其他答案现在已经表明,它可以通过使用单独的lookbehind使用
re
来完成。尽管它与're'有一些有用的区别,但仍然值得一看
regex
。)

我已从正则表达式中更改了您的负面查找。它适用于所提供的输入。不确定它对您的其他人有何反应

print re.sub(r'(?<!Mr)(?<!Esq)(?<!Mrs)(?<!Ms)[.?!]\s+', '\\g<0>\n', s)
print re.sub(r'(?\n',s)

您可以分离出后面的负片外观,以克服像这样的固定宽度图案要求-

r'(?<!Mr)(?<!Mrs)(?<!Ms)(?<!Esq)(?<!e.g)(?<!i.e)[.?]\s+'
r'(?\n',s)
鲍勃先生有1.3磅,他没有多少。
他在乎吗?
乔·布洛格斯,Esq。认为他是。
然而,他停顿了一下。。。
但是,你会怎么处理这个问题呢?
>>> 

谢谢你的回复!为了处理文件,我对声明做了一些修改,效果很好。我忘了在我的示例中包括I.e.或e.g.抱歉!是否可以包括州内的内容?“I.e.他很好”或“e.e.他很好”@user3423572无论您不想拆分什么值,只需在(?感谢老兄!工作完美!:)中添加除最后一个点(.)之外的所有内容即可@user3423572太好了!很高兴提供帮助:)虽然我通过
pip
安装失败,但您是对的。信息不错。