Mega Code Archive

 
Categories / Python Tutorial / Regular Expressions
 

Compiled regular-expression and match objects

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() )