[英]python regex negative lookahead assertion
I'm a cs newbie and am currently working to get a python regex pattern like this: 我是CS新手,目前正在努力获取类似以下的python regex模式:
it must contain "stop (at most 10 words inbetween) mail" and do not contain "mail stop".
that is to say, 也就是说,
"please stop the mail, and I want the mail stop" AND "please stop the mail stop" would be rejected. ("mail stop" pattern spotted)
"please stop the mail" AND "please stop the mail, I want the mail to stop" both would be accepted.(only "stop ~ mail" pattern is seen, and no "mail stop")
what I currently have is: 我目前拥有的是:
import re
pattern = re.compile("(?=(stop\s+(\w+\s+){0,10}mail[^\s]*))(?!mail\s+stop)")
print(pattern.search("please stop the mail, I want the mail to stop").group())
but somehow it doesn't work the way I want. 但以某种方式无法按我想要的方式工作。
Any help would be appreciated. 任何帮助,将不胜感激。
Eric 埃里克
Assuming you need the entire input string to be returned in the event of a match 假设您需要在发生匹配时返回整个输入字符串
>>> pattern = re.compile(".*stop\s+(\w+\s+){0,10}mail(?!(\s+stop|(.*mail stop))).*")
>>> print(pattern.search("please stop the mail, I want the mail to stop"))
<_sre.SRE_Match object at 0x15c43c0>
>>> print(pattern.search("please stop the mail stop"))
None
>>> print(pattern.search("please stop the mail, and I want the mail stop"))
None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.