Python Regex Named Groups — Core Concepts

Named groups extend regex capturing groups with human-readable labels. Instead of accessing matches by number — match.group(1) — you access them by name: match.group('year'). This small change dramatically improves regex maintainability.

For foundational regex syntax, see Python Regex Patterns.

Syntax

Python uses the (?P<name>...) syntax to define a named group. The P stands for Python — this syntax originated in Python before other languages adopted similar features.

FeatureSyntaxExample
Define named group(?P<name>pattern)(?P<year>\d{4})
Backreference by name(?P=name)(?P=word)
Access in replacement\g<name>\g<year>

Accessing Named Matches

After a successful match, named groups are available through several methods:

  • match.group('name') returns the matched text for that group
  • match.groupdict() returns a dictionary of all named groups
  • Named groups also keep their numeric index, so match.group(1) still works

The groupdict() method is especially useful when a pattern captures multiple fields — you get a ready-made dictionary without manual extraction.

When to Use Named Groups

Parsing structured text. Dates, log lines, URLs, and configuration entries all have labeled fields. Named groups make those labels explicit in the pattern.

Substitutions with clarity. In re.sub(), replacement strings can reference named groups with \g<name>, which is far clearer than \1 when you have more than two or three groups.

Named backreferences. The syntax (?P=name) matches the same text a named group already captured. This is handy for detecting repeated words or matching paired delimiters.

Named Groups vs Numbered Groups

FeatureNumberedNamed
Access stylematch.group(1)match.group('host')
ReadabilityIndex must be rememberedSelf-documenting
RefactoringRenumbered if pattern changesStable through edits
Dict outputNoYes via groupdict()

Numbered groups are fine for quick one-off patterns. Named groups pay off the moment you share code or revisit it later.

Interaction with Other Regex Features

Named groups work with all standard regex operations:

  • re.finditer() returns match objects with full named group support
  • re.sub() accepts \g<name> in replacement strings
  • Lookaheads and lookbehinds can contain named groups, though the captures are only accessible if the lookaround succeeds
  • Alternation can have named groups in different branches, but the same name shouldn’t appear in multiple branches in standard re

Common Misconception

“Named groups are just cosmetic.” They’re not. Named groups change how you interact with match results — enabling dictionary output, stable backreferences, and self-documenting replacements. In production codebases, they reduce bugs caused by miscounted group indices after pattern edits.

One Thing to Remember

Named groups transform regex from a write-only language into something you can read six months later — use (?P<name>...) whenever a capture group represents a meaningful field.

pythonregexnamed-groupstext-processing

See Also