mirror of
https://github.com/python/cpython.git
synced 2026-01-06 15:32:22 +00:00
Issue 24454: Improve the usability of the re match object named group API
This commit is contained in:
parent
a3c1728bb6
commit
605bdae078
4 changed files with 82 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue