diff options
author | josh <josh@jrl.ninja> | 2019-04-17 22:43:30 (GMT) |
---|---|---|
committer | Brett Cannon <brettcannon@users.noreply.github.com> | 2019-04-17 22:43:30 (GMT) |
commit | a6de52c74d831e45ee0ff105196da8a58b9e43cd (patch) | |
tree | c34c1cf231f146fa446b1b3e1ac7623e665f1aeb /Doc/howto | |
parent | 4c3efd9cd07194b5db2a60ae5951134cda8b69db (diff) | |
download | cpython-a6de52c74d831e45ee0ff105196da8a58b9e43cd.zip cpython-a6de52c74d831e45ee0ff105196da8a58b9e43cd.tar.gz cpython-a6de52c74d831e45ee0ff105196da8a58b9e43cd.tar.bz2 |
bpo-32913: Added re.Match.groupdict example to regex HOWTO (GH-5821)
Diffstat (limited to 'Doc/howto')
-rw-r--r-- | Doc/howto/regex.rst | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/Doc/howto/regex.rst b/Doc/howto/regex.rst index d385d99..d574c37 100644 --- a/Doc/howto/regex.rst +++ b/Doc/howto/regex.rst @@ -942,6 +942,13 @@ given numbers, so you can retrieve information about a group in two ways:: >>> m.group(1) 'Lots' +Additionally, you can retrieve named groups as a dictionary with +:meth:`~re.Match.groupdict`:: + + >>> m = re.match(r'(?P<first>\w+) (?P<last>\w+)', 'Jane Doe') + >>> m.groupdict() + {'first': 'Jane', 'last': 'Doe'} + Named groups are handy because they let you use easily-remembered names, instead of having to remember numbers. Here's an example RE from the :mod:`imaplib` module:: |