all Python 3refunction
re.Match.groupdict()
groups
Return a dict of the named groups of a Match object — those declared with (?P<name>...); unnamed groups are not included.
Syntax
m.groupdict(default=None)
Names survive edits that numbers do not: inserting a group in the middle of a long pattern shifts every index, but the dict keys stay put. A pattern with no named groups gives an empty dict rather than None or an error, and an optional named group that never participated still appears, carrying the default value. Group names must be valid identifiers and unique within the pattern, or it fails to compile.
Examples
import re
m = re.match(r'(?P<y>\d{4})-(?P<m>\d{2})', '2024-01')
m.groupdict() # → {'y': '2024', 'm': '01'}
m.group('y') # → '2024' (по имени)
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy