4.2.4 MatchObjects

Matchobject instances support the following methods and attributes:

group ([g1, g2, ...])
Returns one or more groups of the match. If there is a single index argument, the result is a single string; if there are multiple arguments, the result is a tuple with one item per argument. If the index is zero, the corresponding return value is the entire matching string; if it is in the inclusive range [1..99], it is the string matching the the corresponding parenthesized group. If no such group exists, the corresponding result is None.

If the regular expression uses the (?P<name>...) syntax, the index arguments may also be strings identifying groups by their group name.

A moderately complicated example:

m = re.match(r"(?P<int>\d+)\.(\d*)", '3.14')
After performing this match, m.group(1) is '3', as is m.group('int'). m.group(2) is '14'.

groups ()
Return a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. Groups that did not participate in the match have values of None. If the tuple would only be one element long, a string will be returned instead.

start (group)

end (group)
Return the indices of the start and end of the substring matched by group. Return None if group exists but did not contribute to the match. For a match object m, and a group g that did contribute to the match, the substring matched by group g (equivalent to m.group(g)) is
    m.string[m.start(g):m.end(g)]
Note that m.start(group) will equal m.end(group) if group matched a null string. For example, after m = re.search('b(c?)', 'cba'), m.start(0) is 1, m.end(0) is 2, m.start(1) and m.end(1) are both 2, and m.start(2) raises an IndexError exception.

span (group)
Return the 2-tuple (start(group), end(group)). Note that if group did not contribute to the match, this is (None, None).

pos
The value of pos which was passed to the search or match function. This is the index into the string at which the regex engine started looking for a match.

endpos
The value of endpos which was passed to the search or match function. This is the index into the string beyond which the regex engine will not go.

re
The regular expression object whose match() or search() method produced this MatchObject instance.

string
The string passed to match() or search().

See Also:

Jeffrey Friedl, Mastering Regular Expressions, O'Reilly. The Python material in this book dates from before the re module, but it covers writing good regular expression patterns in great detail.

guido@CNRI.Reston.Va.US