Compiled regular-expression and match objects. : match « Regular Expressions « Python Tutorial






import re

testString = "Hello world"
formatString = "%-35s: %s"   # string for formatting the output

expression = "Hello"
compiledExpression = re.compile( expression ) 

print formatString % ( "The expression", expression )
print formatString % ( "The compiled expression",compiledExpression )

print formatString % ( "Non-compiled search",re.search( expression, testString ) )
print formatString % ( "Compiled search",compiledExpression.search( testString ) )

print formatString % ( "search SRE_Match contains",re.search( expression, testString ).group() )
print formatString % ( "compiled search SRE_Match contains",compiledExpression.search( testString ).group() )








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