classes and special sequences. : match « Regular Expressions « Python Tutorial






import re

testStrings = [ "2x+5y","7y-3z" ]
expressions = [ r"2x\+5y|7y-3z",r"[0-9][a-zA-Z0-9_].[0-9][yz]", r"\d\w-\d\w" ]

for expression in expressions:
   for testString in testStrings:
      if re.match( expression, testString ):
         print expression, "matches", testString

testString1 = "000-123-4567"
testString2 = "111-123-4567"
testString3 = "email: \t joe_doe@deitel.com"

expression1 = r"^\d{3}-\d{3}-\d{4}$"
expression2 = r"\w+:\s+\w+@\w+\.(com|org|net)"

if re.match( expression1, testString1 ):
   print expression1, "matches", testString1

if re.match( expression1, testString2 ):
   print expression1, "matches", testString2

if re.match( expression2, testString3 ):
   print expression2, "matches", testString3








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