繁体   English   中英

正则表达式:匹配字符串后跟点/逗号后跟空格

[英]Regex: match string followed by dot/comma followed by space

我想把我的text = "hello this is me, and only me. be carefull from 911. "

into: text = "hello this is me,<break></break> and only me.<break></break> be carefull from 911. "

仅适用于后跟点或逗号而不是数字的字符串。

我试过这个表达式: r"\w+([.,])+\s*"但它也匹配数字。

您可以使用

re.sub(r'([^\W\d_][.,])(\s+)', r'\1<break></break>\2', text)

请参阅正则表达式演示

详情

  • ([^\W\d_][.,]) - 第 1 组( \1 ):任何字母,然后是 a . ,
  • (\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'([^\W\d_][.,])(\s+)', r'\1<break></break>\2', text))
# => hello this is me,<break></break> and only me.<break></break> be carefull from 911. 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM