0Pricing
NLP Academy · Lesson

Capturing Groups and Replacements

Pull out and rewrite matched parts.

Capturing Groups and Replacements is a free NLP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Many Matches With Groups

When you use findall with groups, each result becomes a tuple of the captured parts, making structured data easy to collect.

re.findall("(\w+)=(\w+)", "a=1 b=2")

Name Your Groups

You can give a group a name for clearer code. Reach it later with group by that name instead of remembering a number.

re.search("(?P<year>\d{4})", "2026")

Swap Text With re.sub

The re.sub function finds every match and replaces it with text you choose. It is search and replace, powered by patterns.

re.sub("\d+", "#", "a1 b22")

Reuse Captures in Replacements

Inside a replacement you can reference a captured group with \1. This lets you reorder or rewrite matched text on the fly.

re.sub("(\w+) (\w+)", "\2 \1", "hi there")

Redact Sensitive Data

A common use of re.sub is redaction. Match phone numbers or emails, then replace each one with a placeholder like a row of stars.

Replace With a Function

For tricky edits, pass a function to re.sub. It receives each match and returns the replacement, so you can compute results per match.

Non-Capturing Groups

Sometimes you group only to apply a quantifier, not to capture. A non-capturing group starts with ?: so it never clutters your results.

Count Your Replacements

Use re.subn instead of re.sub to get back both the new string and a count of how many replacements it actually made.

re.subn("a", "X", "banana")

Groups Make Data Structured

Capturing groups turn raw text into structured fields. A date string becomes year, month, and day you can store or compare directly.

Quick Check

You matched a full date and want only the year part you wrapped in parentheses. How do you read it?

Recap: Capture and Rewrite

You learned capturing groups, named groups, and re.sub. Together they let you extract precise fields and rewrite text in one clean pass. ✨

Frequently asked questions

Is the “Capturing Groups and Replacements” lesson free?

Yes — the full text of “Capturing Groups and Replacements” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Capturing Groups and Replacements”?

Pull out and rewrite matched parts. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start NLP Academy?

No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Capturing Groups and Replacements” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this NLP Academy lesson?

Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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