Named Capture Group (Date) in PY
Demonstrate named capture groups by extracting year/month/day from ISO-style dates.
Try it in the PY tester →Pattern
regexPY
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}) (flags: g)Python (re) code
pyPython
import re
pattern = re.compile(r"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})")
input_text = "Born 1990-05-21, hired 2018-09-10"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
How the pattern works
(?<year>\d{4}) captures four digits under the name 'year'; (?<month>\d{2}) and (?<day>\d{2}) likewise. Modern JS, Python 3.7+, and Go (with `(?P<...>)` syntax — JS-style is also accepted in Python 3.12+) all support this; the matched substring is accessible by name in code (e.g. m.groups.year in JS, m['year'] in Python).
Examples
Input
Born 1990-05-21, hired 2018-09-10Matches
1990-05-212018-09-10
Input
Today is 2026-04-25Matches
2026-04-25
Input
no dates hereNo match
—