Problem Statement
How do named groups improve regex maintainability? Give an example.
Explanation
Named groups label captured parts, so your code reads by meaning rather than index numbers. This reduces off-by-one errors and makes future edits safer.
You can access groups by name in match dictionaries, which is clearer for complex patterns such as dates, ids, or log lines.
Code Solution
SolutionRead Only
m=re.search(r'(?P<yyyy>\d{4})-(?P<mm>\d{2})-(?P<dd>\d{2})', s)
year=m.group('yyyy')Practice Sets
This question appears in the following practice sets:
