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
Basic Capturing Group
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()
import re
m = re.search(r'(\w+)@(\w+)\.(\w+)', 'user@example.com')
if m: print(m.groups())Named Groups
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
import re
print(re.findall(r'(?:cat|dog)s?', 'cats dogs cat dog'))Backreferences
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
import re
result = re.findall(r'(\w+)=(\w+)', 'x=1 y=2 z=3')
print(result)Using Groups in re.sub()
import re
result = re.sub(r'(\w+)\s(\w+)', r'\2 \1', 'John Smith')
print(result)Lookahead (?= )
import re
# Prices followed by USD
print(re.findall(r'\d+(?= USD)', '100 USD 200 EUR 300 USD'))Lookbehind (?<= )
import re
print(re.findall(r'(?<=\$)\d+', 'paid $100 and $200'))Named Backreference
import re
m = re.search(r'<(?P<tag>\w+)>.*?</(?P=tag)>', '<b>hello</b>')
if m: print(m.group())Quick Check
Recap
Keep Going
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
- Regex Fundamentals
- Using re.match, re.search, re.findall
- Groups and Named Captures
- re.sub, Flags, and Compiled Patterns