我想打开我的text = "hello this is me, and only me. be carefull from 911. "
进入:text = "hello this is me,<break></break> and only me.<break></break> be carefull from 911. "
仅适用于后面跟着句点或逗号的字符串,而不是数字。
我试着用这个表达式:r"w+([.,])+s*"
,但它也匹配数字。
您可以使用
re.sub(r'([^Wd_][.,])(s+)', r'1<break></break>2', text)
请参阅regex演示。
详细信息:
([^Wd_][.,])
-组1(1
(:任何字母,然后是.
或,
(s+)
-组2(2
(:一个或多个空白字符
请参阅Python演示:
import re
text = "hello this is me,<break></break> and only me.<break></break> be carefull from 911. "
print(re.sub(r'([^Wd_][.,])(s+)', r'1<break></break>2', text))
# => hello this is me,<break></break> and only me.<break></break> be carefull from 911.