Repetition patterns, matching vs searching. : match « Regular Expressions « Python Tutorial






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 ]








16.4.match
16.4.1.Matching Strings with match()
16.4.2.Here is an example of a failed match where None is returned:
16.4.3.Repetition, Special Characters, and Grouping
16.4.4.Compiled regular-expression and match objects.
16.4.5.Repetition patterns, matching vs searching.
16.4.6.classes and special sequences.
16.4.7.Dig out path