我正在捕获一个在文本消息中传递的字符串。该条件基于单词";接触";。
以下是三个示例文本:
ntpd process is not running on lnx31 contact: app-support. @monitoringautomation
ntpd process is not running on lnx31 contact: app-support, @monitoringautomation
ntpd process is not running on lnx31 contact app-support @monitoringautomation
我当前的正则表达式是:
/(?i)contact:* (S+),|(?i)contact:* (S+).|(?i)contact:* (S+)s*/gm
我的问题是,有没有其他方法可以清理或缩短这个表达式?我尝试过下面的例子,但当使用句点或逗号时,它不会捕获应用程序团队,而是将其包含在匹配中。
/((?i)contact:* (S+)(,|.|s*))/gm
您可以使用
(?i)bcontact:*s*([^,.s]+)
请参阅regex演示。
详细信息:
(?i)
-不区分大小写的内联修饰符选项b
-一个词的边界contact
-字符串contact
:*
-零个或多个冒号s*
-零个或多个空白([^,.s]+)
-第1组:除空白、逗号和句点之外的一个或多个字符
查看Python演示:
import re
text = """ntpd process is not running on lnx31 contact: app-support. @monitoringautomation
ntpd process is not running on lnx31 contact: app-support, @monitoringautomation
ntpd process is not running on lnx31 contact app-support @monitoringautomation"""
print( re.findall(r"(?i)bcontact:*s*([^,.s]+)", text) )
# => ['app-support', 'app-support', 'app-support']