正则表达式用于排除和包含标准



我需要编写一个正则表达式,如果它包含字符串,它可以排除文本,但如果该字符串不存在并且文本包含特定字符串,则可以保留文本。对于以下文本

text1 = "unable to reach and confirm the first choice of attempts made, vm last night left with call back number re soc poc."

如果文本包含"无法到达",我想排除文本,但如果"无法到达"不存在并且文本包含"vm 左侧",则保留文本。

这是我的代码:

(b^(?!(unable to reach))(?:(note|vm)(?:W+w+){0,3}?W+(left|leave))b, text1)

它包括文本 1,但对于文本 2 无法识别"vm 左侧",而"无法访问"不存在。

text2 = " confirm the first choice of attempts made, vm last night left with call back number re soc poc."

请帮忙。

pattern = re.compile('^(?!unable to reach).*(?:vm|note).*(?:left|leave).*', flags=re.IGNORECASE) 
re.findall(pattern, text1)

这是代码:

^(?=.*(?:(note|vm)(?:W+w+){0,3}?W+(left|leave)))(?!.*(?:unable to reach))

您可以在 https://regex101.com 中检查它

最新更新