I am using regex to match strings, here is my code:
r = re.compile(r"#?%s\b" % "apple", re.IGNORECASE|re.MULTILINE)
if r.search("I am eating an apple!"):
print "success"
The "!" at the end of apple is causing it not to match... is there a way I can ignore punctuation such as ? ! at the end of the word?
Your sample code works for me: http://repl.it/J0t/5
The string formatting operator creates #?apple\\b
the #?
is pretty useless but will allow the expression to match all the following:
#apple
apple
grapple
apple\\b
will also match the same.
If your task is to validate you have a whole word apple which is not part of another word, then I recommend something like \\bapple\\b
which matches only #apple
and apple
from the samples above.
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.