Issue 24454: Improve the usability of the re match object named group API

This commit is contained in:
Eric V. Smith 2016-09-11 08:55:43 -04:00
parent a3c1728bb6
commit 605bdae078
4 changed files with 82 additions and 1 deletions

View file

@ -1015,6 +1015,22 @@ Match objects support the following methods and attributes:
'c3'
.. method:: match.__getitem__(g)
This is identical to ``m.group(g)``. This allows easier access to
an individual group from a match:
>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m[0] # The entire match
'Isaac Newton'
>>> m[1] # The first parenthesized subgroup.
'Isaac'
>>> m[2] # The second parenthesized subgroup.
'Newton'
.. versionadded:: 3.6
.. method:: match.groups(default=None)
Return a tuple containing all the subgroups of the match, from 1 up to however