I’m using this script:
import re
message = 'oh, hey there'
matches = ['hi', 'hey', 'hello']
def process(message):
for item in matches:
search = re.match(item, message)
if search == None:
return False
return True
print process(message)
Basically, my aim is to check if any part of the message is inside any of the items in matches, however using this script, it always returns False (no match).
Could someone point out if I am doing something wrong in this code?
Use
searchrather thanmatch. As an optimization,matchonly starts looking at the beginning of the string, rather than anywhere in it.Additionally, you’re only looking at the result of the last match attempt. You should check inside of the loop and return early if any match:
Note that if you only care about substrings and don’t need to match on regular expressions, just use the
inoperator: