示例:
string_1 = 'Hello,How are you...?'
string_2 = 'I am having problem....using this device..'
我想删除多余的 '.'
。我想要像 find&replace
这样的东西。我想为此包括 RegEx,但并不擅长。
我尝试了以下方法:
import re
v = re.sub('.', '.', string_1)
但这并没有给我我想要的。如何操作re.sub()
中的第一个'.'
?
最佳答案
您可以在正则表达式中使用 +
并否定 .
因为它匹配正则表达式中的所有内容
代码:
re.sub("\.+", ".", string_2)
输出:
'I am having problem.using this device.'
关于python - 如何在处理前从字符串中去除额外的句号 (.) 或清理数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34606695/