Lesson 7 · Python standard library deep dive
Python re Explained: Regular Expressions Made Simple | Standard Library #7
Re, Python's pattern-matching engine for text. Matching, searching, groups, substitution, compiling, and flags. Let's get into it. match, search, and…
- CoursePython standard library deep dive
- Lesson7 of 24
- Video18 min
- FormatJupyter notebook · 15 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbPython Standard Library Deep-Dive, Video 7: re (Regular Expressions)#
- Video seven of the twenty-five-part series: re, Python's pattern-matching engine for text.
- Matching, searching, groups, substitution, compiling, and flags.
- Let's get into it.
Part 1: What re Offers#
import re
text = 'The year 2026 was eventful'
match = re.search(r'\d+', text)
print(match)
print(match.group())
Part 2: match(), search(), fullmatch()#
text = 'hello world'
print(re.match(r'hello', text))
print(re.match(r'world', text))
print(re.search(r'world', text))
print(re.fullmatch(r'hello world', text))
print(re.fullmatch(r'hello', text))
Part 3: findall() and finditer()#
text = 'Order 12 has 5 items, order 45 has 2 items'
numbers = re.findall(r'\d+', text)
print(numbers)
for m in re.finditer(r'\d+', text):
print(m.group(), m.start(), m.end())
Part 4: Character Classes and Quantifiers#
text = 'Call 555-1234 or email me@test.com, id_007 works too'
print(re.findall(r'\d+', text))
print(re.findall(r'\w+', text))
print(re.findall(r'\s+', text))
print(re.findall(r'[aeiou]', 'hello world'))
print(re.findall(r'[^aeiou\s]', 'hello world'))
print(re.findall(r'ab?c', 'ac abc abbc'))
print(re.findall(r'ab*c', 'ac abc abbbc'))
print(re.findall(r'ab+c', 'ac abc abbbc'))
print(re.findall(r'\d{3}', '12 123 1234 12345'))
print(re.findall(r'\d{2,4}', '12 123 1234 12345'))
Part 5: Anchors and Boundaries#
print(re.findall(r'^\d+', '123 abc 456'))
print(re.findall(r'\d+$', '123 abc 456'))
print(re.findall(r'\bcat\b', 'cat category cats cat'))
print(re.findall(r'cat', 'cat category cats cat'))
Part 6: Groups and Capturing#
text = 'Date: 2026-03-15'
match = re.search(r'(\d{4})-(\d{2})-(\d{2})', text)
print(match.group())
print(match.group(0))
print(match.group(1))
print(match.groups())
match = re.search(r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})', text)
print(match.group('year'))
print(match.group('month'))
print(match.groupdict())
Part 7: sub() and subn()#
text = 'Contact: 555-1234 or 555-5678'
redacted = re.sub(r'\d{3}-\d{4}', 'XXX-XXXX', text)
print(redacted)
result, count = re.subn(r'\d{3}-\d{4}', 'XXX-XXXX', text)
print(result, count)
text = 'John Smith, Jane Doe'
swapped = re.sub(r'(\w+) (\w+)', r'\2 \1', text)
print(swapped)
def uppercase_match(m):
return m.group().upper()
shouted = re.sub(r'\w+', uppercase_match, text)
print(shouted)
Part 8: split() with Regex#
text = 'apple, banana; cherry,date'
print(re.split(r'[,;]\s*', text))
print(text.split(','))
text2 = 'one1two22three333four'
print(re.split(r'\d+', text2))
Part 9: Compiling Patterns for Reuse#
phone_pattern = re.compile(r'\d{3}-\d{3}-\d{4}')
text1 = 'Call 555-123-4567'
text2 = 'Or reach 555-987-6543 instead'
print(phone_pattern.search(text1).group())
print(phone_pattern.search(text2).group())
print(phone_pattern.findall('555-111-2222 and 555-333-4444'))
Part 10: Flags: IGNORECASE, MULTILINE, DOTALL#
print(re.findall(r'python', 'Python PYTHON python', re.IGNORECASE))
text = 'line one\nline two\nline three'
print(re.findall(r'^line', text))
print(re.findall(r'^line', text, re.MULTILINE))
print(re.findall(r'one.two', 'one\ntwo'))
print(re.findall(r'one.two', 'one\ntwo', re.DOTALL))
Part 11: Common Patterns#
email_pattern = re.compile(r'^[\w.+-]+@[\w-]+(?:\.[\w-]+)+$')
candidates = ['user@example.com', 'not-an-email', 'a.b+c@sub.domain.co']
for candidate in candidates:
is_valid = bool(email_pattern.match(candidate))
print(candidate, is_valid)
report = 'Revenue: $45,231.50 up from $38,900.00 last quarter, a 16.28% increase'
numbers = re.findall(r'\d[\d,]*\.?\d*', report)
print(numbers)
cleaned = [float(n.replace(',', '')) for n in numbers]
print(cleaned)
Wrap-Up: What You Learned#
- match, search, and fullmatch, each anchoring differently.
- findall for a plain list of matches, finditer for match objects with position data.
- Character classes, quantifiers, anchors, and word boundaries.
- Capturing groups, both positional and named, and how to pull them apart.
- sub and subn for substitution, including function-based and backreference replacements.
- re.split for pattern-based splitting, and re.compile for reusable, faster patterns.
- Flags: IGNORECASE, MULTILINE, DOTALL.
- Two real patterns: email validation and extracting numbers from mixed text.
- That wraps up re. Next up: json and csv, for structured data serialization.
🧠 Test yourself
Pick an answer to check it. Your best score is saved in this browser.
What does this print?
import re print(re.match(r'world', 'hello world'))
match only checks the START of the string, and 'hello world' does not begin with 'world'.
What does this print?
import re print(re.findall(r'\d{2,4}', '12 123 1234 12345'))
{2,4} is greedy up to 4 digits, so '12345' gives '1234' and the leftover '5' is too short to match.
What does this print?
import re print(re.sub(r'(\w+) (\w+)', r'\2 \1', 'John Smith'))
\1 and \2 refer to the captured groups, so the replacement swaps them.
What does this print?
import re print(re.findall(r'\bcat\b', 'cat category cats cat'))
\b marks a word boundary, so 'category' and 'cats' do not match as whole words.
What does this print?
import re text = 'line one\nline two' print(re.findall(r'^line', text))
Without re.MULTILINE, ^ only matches at the very start of the whole string.
💻 Practice exercises
Try each one in the notebook or in your own editor before opening the solution.
Extract all whole numbers from 'Order 12 has 5 items, order 45 has 2 items' and print their sum.
💡 Hint
Use re.findall(r'\d+', text) and convert to int.
✅ Show solution
import re text = "Order 12 has 5 items, order 45 has 2 items" numbers = [int(n) for n in re.findall(r"\d+", text)] print(numbers, sum(numbers)) assert sum(numbers) == 64
Use a named-group pattern to pull year, month and day out of 'Date: 2026-03-15' and print the groupdict. Then reformat the date as DD/MM/YYYY with re.sub.
💡 Hint
Use (?P<year>\d{4}) style groups, and \3/\2/\1 in the replacement.
✅ Show solution
import re text = "Date: 2026-03-15" m = re.search(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})", text) print(m.groupdict()) swapped = re.sub(r"(\d{4})-(\d{2})-(\d{2})", r"\3/\2/\1", text) print(swapped) assert swapped == "Date: 15/03/2026"
Given a few log lines, parse each with a compiled regex into level and message, count lines per level, and mask any email addresses in messages as '[email]' using re.sub.
💡 Hint
Compile r'^(?P<level>[A-Z]+): (?P<msg>.*)$' and use a simple email pattern for the mask.
✅ Show solution
import re from collections import Counter lines = [ "INFO: user ana@example.com logged in", "ERROR: payment failed for sam@test.org", "INFO: cache warmed", ] line_re = re.compile(r"^(?P<level>[A-Z]+): (?P<msg>.*)$") email_re = re.compile(r"[\w.+-]+@[\w-]+(?:\.[\w-]+)+") levels = Counter() for line in lines: m = line_re.match(line) levels[m["level"]] += 1 print(m["level"], email_re.sub("[email]", m["msg"])) print(levels) assert levels == {"INFO": 2, "ERROR": 1}
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



