import re
testStrings = [ "Heo", "Helo", "Hellllo" ] expressions = [ "Hel?o", "Hel+o", "Hel*o" ]
# match every expression with every string for expression in expressions: for string in testStrings: if re.match( expression, string ): print expression, "matches", string else: print expression, "does not match", string print
# demonstrate the difference between matching and searching expression1 = "elo" # plain string expression2 = "^elo" # "elo" at beginning of string expression3 = "elo$" # "elo" at end of string
if re.match( expression1, testStrings[ 1 ] ): print expression1, "matches", testStrings[ 1 ]
if re.search( expression1, testStrings[ 1 ] ): print expression1, "found in", testStrings[ 1 ]
if re.search( expression2, testStrings[ 1 ] ): print expression2, "found in", testStrings[ 1 ]
if re.search( expression3, testStrings[ 1 ] ): print expression3, "found in", testStrings[ 1 ]
|