I'm a cs newbie and am currently working to get a python regex pattern like this:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.