Capturing Groups
Parentheses create capturing groups. Use match.group(1) to access them, or match.groups() for all captures.
Parentheses create capturing groups. Use match.group(1) to access them, or match.groups() for all captures.
import re
pattern = r"(\d{4})-(\d{2})-(\d{2})"
match = re.search(pattern, "Date: 2024-07-15")
if match:
year, month, day = match.groups()
print(year, month, day) # 2024 07 15
Named groups (?P