[英]Regex: match string followed by dot/comma followed by space
i want to turn my text = "hello this is me, and only me. be carefull from 911. "
我想把我的
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. "
into:
text = "hello this is me,<break></break> and only me.<break></break> be carefull from 911. "
only for strings followed by dot or comma not number.仅适用于后跟点或逗号而不是数字的字符串。
i tried with this expression: r"\w+([.,])+\s*"
but it match also numbers.我试过这个表达式:
r"\w+([.,])+\s*"
但它也匹配数字。
You can use您可以使用
re.sub(r'([^\W\d_][.,])(\s+)', r'\1<break></break>\2', text)
See the regex demo .请参阅正则表达式演示。
Details :详情:
([^\W\d_][.,])
- Group 1 ( \1
): any letter and then a .
([^\W\d_][.,])
- 第 1 组( \1
):任何字母,然后是 a .
or ,
,
(\s+)
- Group 2 ( \2
): one or more whitespace chars. (\s+)
- 第 2 组 ( \2
):一个或多个空白字符。 See the Python demo :请参阅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.