Python正则表达式将空间置于非重复时期之前

时间:2016-03-31 02:01:02

标签: python regex

例如给出字符串:

Reading my book... hate it... The childs is good read. This is funny.

它应该返回:

Reading my book... hate it... The childs is good read . This is funny .

请注意d.之间的空格。

1 个答案:

答案 0 :(得分:0)

使用negative lookaround assertion,您可以过滤不遵循点(.)且后面没有点((?<!\.))的(?!\.)

>>> text = 'Reading my book... hate it... The childs is good read. This is funny.'
>>> import re
>>> re.sub(r'(?<!\.)\.(?!\.)', r' .', text)  # Replace the matched dot with ` .`
'Reading my book... hate it... The childs is good read . This is funny .'