Python 排除和包含标准的正则表达式

Python 排除和包含标准的正则表达式,python,regex,Python,Regex,我需要编写一个正则表达式,它可以排除包含字符串的文本,但如果该字符串不存在且文本包含特定字符串,则保留该文本。下文 text1 = "unable to reach and confirm the first choice of attempts made, vm last night left with call back number re soc poc." 我想排除包含“无法到达”的文本,但如果“无法到达”不存在且文本包含“vm left”,则保留文本 这是我的代码

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

text1 = "unable to reach and confirm the first choice of attempts made, vm last night left with call back number re soc poc."
我想排除包含“无法到达”的文本,但如果“无法到达”不存在且文本包含“vm left”,则保留文本

这是我的代码:

(\b^(?!(unable to reach))(?:(note|vm)(?:\W+\w+){0,3}?\W+(left|leave))\b, text1)
它包括文本1,但对于文本2,当“无法到达”不存在时,无法识别“vm left”

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

这是代码:

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

你可以在

中查看为什么投反对票?它有什么问题?当您省略锚时
\b(?!无法到达)(注意| vm)(?:\W+\W+{0,3}?\W+(左|左)\b
您所说的“排除文本”和“包含文本”是什么意思?您的意思是要确定字符串是否包含
“vm”
“left”
,而不包含
“无法到达”
?您说过它必须包含字符串
“vm left”
,它与
“vm”
“left”
不同。“注意”和“离开”是从哪里来的?你需要修改你的问题来澄清这些问题。你能解释一下regexp是如何解决这个问题的吗?只有代码的答案对于未来的读者来说没有那么有用。
pattern = re.compile('^(?!unable to reach).*(?:vm|note).*(?:left|leave).*', flags=re.IGNORECASE) 
re.findall(pattern, text1)