The re Module (cont)

Regular Expression Objects

  • Objects created by re.compile() have these methods
     r.search(s [,pos [,endpos]])   # Search for a match
     r.match(s [,pos [,endpos]])    # Check string for match
     r.split(s)                     # Split on a regex match
     r.findall(s)                   # Find all matches
     r.sub(repl,s)                  # Replace all matches with repl 
  • When a match is found a 'MatchObject' object is returned.
  • This contains information about where the match occurred.
  • Also contains group information.

Notes

  • The search method looks for a match anywhere in a string.
  • The match method looks for a match starting with the first character.
  • The pos and endpos parameters specify starting and ending positions for the search/match.
<<< O'Reilly OSCON 2000, Advanced Python Programming, Slide 28
July 17, 2000, beazley@cs.uchicago.edu
>>>