Capturing Groups and Replacements
Pull out and rewrite matched parts.
Grab Parts of a Match
A capturing group wraps part of a pattern in parentheses so you can pull out just that piece, not the whole match.
re.search("(\d+)-(\d+)", "12-34")Reading Groups by Number
After a match, group(1) returns the first parenthesized part, group(2) the second, and group(0) the entire matched string.
m.group(1) # first groupAll lessons in this course
- Regex in 5 Minutes
- Finding Emails and URLs
- Capturing Groups and Replacements
- Regex Tokenization Tricks