仅替换句子点而不替换小数点

时间:2020-09-30 19:04:10

标签: python-3.x regex

比方说:

s = "this is a sentence. My GPA is 1.10. Thanks to StackOverflow, otherwise I would have a lower one."

我想要以下内容:

new_s = "this is a sentence my gpa is 1.10 Thanks to StackOverflow otherwise I would have a lower one"

这就是我要做的:

new_s = s.lower()
new_s = new_s.replace(',', '')

我苦苦挣扎的是替换1.10中的要点

如果我这样做:

new_s = new_s.replace(',', '')

我不会得到想要的输出。有没有办法只选择想要的点(不用来表示十进制数字)?

1 个答案:

答案 0 :(得分:1)

在正则表达式中将re.sub()negative lookahead一起使用。

import re

new_s = re.sub(r'\.(?!\d)', '', s)

这与.匹配,后面没有数字。