简体   繁体   English

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

[英]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.

相关问题 正则表达式匹配字符串,其中单词后跟空格,然后是数字点或连字符,单词后跟空格,然后(一些信息) - Regex to match string which has words followed by whitespace then digits dot or hyphen and words followed by space and then (some info) 用逗号分割字符串,除非后跟空格或“+” - Split string by comma unless followed by a space or a '+' 正则表达式匹配标点符号,后跟空格,但有一些例外 - Regex to match punctuation followed by space with some exceptions 正则表达式匹配字符串,后跟任何单词,后跟字符串变量 - regex match string followed by any word followed by string variable python regex匹配数字,后跟字符串或不包含任何内容 - python regex match number followed by string or nothing 如果后面没有模式,则正则表达式匹配 - regex match if not followed by pattern Python正则表达式:仅在逗号后没有空格的情况下添加空格 - Python regex : adding space after comma only if not followed by a number 正则表达式定位数字后跟空格后跟字符 - Regex locate number followed by space followed by character 仅当带空格,句点或什么都没有正则表达式时,才使用Python匹配字符串中的字母吗? - Use Python to match a letter in a string only when followed by a space, period, or nothing, without regex? 正则表达式匹配一系列单个字符后跟单个空格 - regex to match a series of single Character followed by a single space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM