CKTIS / Python
re.Match.groupdict — a dictionary of named groups
re.Match.groupdict returns a dictionary of named capture groups. Its default parameter supplies the value for groups that did not participate in the match.
Version: Python 3.14; repository image, production runtime not attested
Syntax
match.groupdict(default=None)Call re.Match.groupdict on a match object. It returns a dictionary mapping names from (?P<name>...) groups to captured values. Unnamed groups are excluded; without named groups, it returns {}.
The default parameter defaults to None. It replaces only values for groups that did not participate, not matched empty strings ''.
First check that the search did not return None: no match is not an empty dictionary. In the example, the optional tag group does not participate. sorted(...items()) makes the display order explicit.
Examples
import re
match = re.fullmatch(r"(?P<name>[A-Za-z]+)(?:-(?P<tag>[0-9]+))?", "Ada")
if match is None:
raise ValueError("No match")
print(sorted(match.groupdict().items()))
print(sorted(match.groupdict(default="missing").items()))Result
[('name', 'Ada'), ('tag', None)]
[('name', 'Ada'), ('tag', 'missing')]
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.