1

我想要匹配字符串,例如:

what.are you doing?

当点后面有一些字母时匹配 并替换“。” 和 ”。 ”

结果如下:
what. are you doing?

更多示例

输入

uh....i don't know

输出

uh.... i don't know

输入

do you know....that thing?

输出

do you know.... that thing?
4

2 回答 2

7

这应该适合您的情况:

re.sub(r'(?<=\S)\.(?=\w)', '. ', s)

这使用了一个肯定的lookbehind断言(?<=...)来检查点前面是否有一个非空格字符\S。肯定的lookbehind 断言(?=\w)检查点后是否跟一个字母数字字符。

于 2013-04-28T13:56:41.547 回答
0

我在这方面已经很晚了。不过,想分享另一个选择:

re.sub(r'\.(\w)', '. \\1', s)

Explanation:
\.(\w)
|   |
|   ----> catch an alphabet letter in group '()' which is referenced later in replacement by \\1 (because it's 1st caught group)
escape '.'

希望能帮助到你 :)

于 2016-07-18T06:03:27.377 回答