Repetition patterns, matching vs searching. : Match : Regular Expressions PYTHON TUTORIALS


PYTHON TUTORIALS » Regular Expressions » Match »

 

Repetition patterns, matching vs searching.


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.matchexpression, 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.matchexpression1, testStrings] ):
   print expression1, "matches", testStrings]

if re.searchexpression1, testStrings] ):
   print expression1, "found in", testStrings]

if re.searchexpression2, testStrings] ):
   print expression2, "found in", testStrings]

if re.searchexpression3, testStrings] ):
   print expression3, "found in", testStrings]



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Regular Expressions
» Match