0Pricing
Python Academy · Lesson

Groups and Named Captures

Extract data with capturing groups and named group syntax.

Groups and Named Captures is a free Python 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 Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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))

m.groups()

m.groups() returns all capture groups as a tuple.
import re
m = re.search(r'(\w+)@(\w+)\.(\w+)', 'user@example.com')
if m: print(m.groups())

Named Groups

(?Ppattern) creates a named group. Access with m.group('name') or m.groupdict().
import re
m = re.search(
    r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})',
    '2024-01-15'
)
if m: print(m.groupdict())

Non-Capturing Groups

(?:pattern) groups without capturing. Useful when you need grouping for quantifiers but don't need the match.
import re
print(re.findall(r'(?:cat|dog)s?', 'cats dogs cat dog'))

Backreferences

\1 inside a pattern refers to what group 1 matched. Useful for finding repeated words.
import re
# Match repeated words
m = re.search(r'\b(\w+)\s+\1\b', 'the the quick brown', re.IGNORECASE)
if m: print('repeated:', m.group())

findall with Groups

When the pattern has groups, findall returns a list of group tuples, not full matches.
import re
result = re.findall(r'(\w+)=(\w+)', 'x=1 y=2 z=3')
print(result)

Using Groups in re.sub()

In re.sub(), \1 or \g inserts captured groups into the replacement string.
import re
result = re.sub(r'(\w+)\s(\w+)', r'\2 \1', 'John Smith')
print(result)

Lookahead (?= )

(?=expr) is a positive lookahead — matches position where expr follows, without consuming.
import re
# Prices followed by USD
print(re.findall(r'\d+(?= USD)', '100 USD 200 EUR 300 USD'))

Lookbehind (?<= )

(?<=expr) is a positive lookbehind — matches position preceded by expr, without consuming.
import re
print(re.findall(r'(?<=\$)\d+', 'paid $100 and $200'))

Named Backreference

(?P=name) in the pattern references a named group's value. Useful for matching closing tags.
import re
m = re.search(r'<(?P<tag>\w+)>.*?</(?P=tag)>', '<b>hello</b>')
if m: print(m.group())

Quick Check

How do you access a named capture group called 'year' from a match object m?

Recap

Groups () capture parts. m.group(1), m.groups(), m.groupdict(). Named: (?P). Non-capturing: (?:). Backrefs: \1 or \g in replacements.

Keep Going

Great work! The next lesson awaits.

Frequently asked questions

Is the “Groups and Named Captures” lesson free?

Yes — the full text of “Groups and Named Captures” is free to read here on the web, and the Python 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 Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Groups and Named Captures”?

Extract data with capturing groups and named group syntax. You practise Python 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 Python Academy?

No prior experience is required. Python 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 “Groups and Named Captures” 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 Python Academy lesson?

Yes. Every Python 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 Fundamentals
  2. Using re.match, re.search, re.findall
  3. Groups and Named Captures
  4. re.sub, Flags, and Compiled Patterns
← Back to Python Academy