SyntaxStudy
Sign Up
Python Intermediate 4 min read

Capturing Groups

Capturing Groups

Parentheses create capturing groups. Use match.group(1) to access them, or match.groups() for all captures.

Example
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
Pro Tip

Named groups (?P...) make patterns more readable.