line = "This is a text.This is another text,it has no space after the comma."
re.sub(r'(?<=[.,])(?=[^\s])', r' ', line)
// Output: 'This is a text. This is another text, it has no space after the comma.'
网友
2楼 ·
发布于 2024-05-19 12:03:18
或者,也可以不使用regex来解决您的问题,如下所示:
>>> line = "This is a text.This is another text,it has no space after the comma."
>>> line.replace('.', '. ', line.count('.')).replace(',', ', ', line.count(','))
'This is a text. This is another text, it has no space after the comma. '
>>>
使用此正则表达式可以匹配前面的字符是一个点或逗号而下一个字符不是空格的位置:
(?<=[.,])
正向查找后面的点或逗号(?=[^\s])
与任何不是空格的内容匹配的正向展望所以这将匹配逗号后面的位置,或者像
ext.This
或text,it
这样的空格。但不是word. This
。替换为单个空格(
)
Regex101 Demo
Python:
或者,也可以不使用regex来解决您的问题,如下所示:
相关问题 更多 >
编程相关推荐