Once you have an object representing a compiled regular expression, what do you do with it? RegexObject instances have several methods and attributes. Only the most significant ones will be covered here; consult the Library Reference for a complete listing.
||Determine if the RE matches at the beginning of the string.|
||Scan through a string, looking for any location where this RE matches.|
||Split the string into a list, splitting it wherever the RE matches|
||Find all substrings where the RE matches, and replace them with a different string|
||Does the same thing as sub(), except you can limit the number of replacements|
These methods return None if no match can be found. If they're successful, a MatchObject instance is returned, containing information about the match: where it starts and ends, the substring it matched, and more.
You can learn about this by interactively experimenting with the re module. (If you have Tkinter available, you may also want to look at regexdemo.py, a demonstration program included with the Python distribution. It allows you to enter REs and strings, and displays whether the RE matches or fails. regexdemo.py can be quite useful when trying to debug a complicated RE.)
First, run the Python interpreter, import the re module, and compile a RE:
Python 1.5.1 (#6, Jul 17 1998, 20:38:08) [GCC 220.127.116.11] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> import re >>> p = re.compile('[a-z]+') >>> p <re.RegexObject instance at 80c3c28>
>>> p.match( "" ) >>> print p.match( "" ) None
>>> m = p.match( 'tempo') >>> print m <re.MatchObject instance at 80c4f68>
||Return the string matched by the RE|
||Return the starting position of the match|
||Return the ending position of the match|
||Return a tuple containing the (start, end) of the match|
Trying these methods will soon clarify their meaning:
>>> m.group() 'tempo' >>> m.start(), m.end() (0, 5) >>> m.span() (0, 5)
>>> print p.match('::: message') None >>> m = p.search('::: message') ; print m <re.MatchObject instance at 80c9650> >>> m.group() 'message' >>> m.span() (4, 11)
p = re.compile( ... ) m = p.match( 'string goes here' ) if m: print 'Match found: ', m.group() else: print 'No match'