0Pricing
NLP Academy · Lesson

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 group

All lessons in this course

  1. Regex in 5 Minutes
  2. Finding Emails and URLs
  3. Capturing Groups and Replacements
  4. Regex Tokenization Tricks
← Back to NLP Academy