Groups and Named Captures
Extract data with capturing groups and named group syntax.
Introduction
Capture groups extract specific parts of a match. Named groups make the extracted data self-documenting.
Basic Capturing Group
Parentheses () create a capturing group. m.group(1) returns the first group's match.
import re
m = re.search(r'(\d{4})-(\d{2})-(\d{2})', '2024-01-15')
if m: print(m.group(1), m.group(2), m.group(3))All lessons in this course
- Regex Fundamentals
- Using re.match, re.search, re.findall
- Groups and Named Captures
- re.sub, Flags, and Compiled Patterns