Mega Code Archive

 
Categories / Python Tutorial / Regular Expressions
 

Classes and special sequences

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