MatchObject instances support the following methods and attributes:
-1. If a group is contained in a part of the pattern that matched multiple times, the last match is returned.
If the regular expression uses the (?P<name>...) syntax, the groupN arguments may also be strings identifying groups by their group name. If a string argument is not used as a group name in the pattern, an IndexError exception is raised.
A moderately complicated example:
m = re.match(r"(?P<int>\d+)\.(\d*)", '3.14')
After performing this match,
'3', as is
None. (Incompatibility note: in the original Python 1.5 release, if the tuple was one element long, a string would be returned instead. In later versions (from 1.5.1 on), a singleton tuple is returned in such cases.)
-1if 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.start(group) will equal
group matched a null string. For example, after
m.start(0) is 1,
m.end(0) is 2,
m.end(1) are both 2, and
an IndexError exception.
(m.start(group), m.end(group)). Note that if group did not contribute to the match, this is
(-1, -1). Again, group defaults to zero.
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.