Skip to content

Commit a6de52c

Browse files
joshbrettcannon
authored andcommitted
bpo-32913: Added re.Match.groupdict example to regex HOWTO (GH-5821)
1 parent 4c3efd9 commit a6de52c

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

Doc/howto/regex.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,13 @@ given numbers, so you can retrieve information about a group in two ways::
942942
>>> m.group(1)
943943
'Lots'
944944

945+
Additionally, you can retrieve named groups as a dictionary with
946+
:meth:`~re.Match.groupdict`::
947+
948+
>>> m = re.match(r'(?P<first>\w+) (?P<last>\w+)', 'Jane Doe')
949+
>>> m.groupdict()
950+
{'first': 'Jane', 'last': 'Doe'}
951+
945952
Named groups are handy because they let you use easily-remembered names, instead
946953
of having to remember numbers. Here's an example RE from the :mod:`imaplib`
947954
module::
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added re.Match.groupdict example to regex HOWTO.

0 commit comments

Comments
 (0)