Skip to content

Regular Expressions

Match and extract text patterns with the re module: search, match, groups, and substitution.

What you'll learn

  • Use re.search, re.match, and re.findall
  • Capture groups with parentheses
  • Apply quantifiers, anchors, and character classes
  • Substitute text with re.sub
  • Compile patterns for reuse and performance

Concept

The re Module

Python's re module provides regular expression support. A regex is a pattern that describes a set of strings — useful for validation, extraction, and substitution:

import re

# Search for a pattern in a string
match = re.search(r"\d{3}", "Call 555-1234 for help")
if match:
    print(match.group())  # 555
    print(match.start())  # 5 (position where match starts)

Always use raw strings (r"...) for regex patterns — without the r` prefix, Python interprets backslash escapes before the regex engine sees them, causing subtle bugs.

re.search vs re.match vs re.fullmatch

  • re.search(pattern, string) — find the pattern anywhere in the string
  • re.match(pattern, string) — match only at the beginning of the string
  • re.fullmatch(pattern, string) — the entire string must match
import re

text = "Hello, World!"

print(re.search(r"World", text))   # <Match object> — found
print(re.match(r"Hello", text))    # <Match object> — matches at start
print(re.match(r"World", text))    # None — not at the start
print(re.fullmatch(r"Hello, World!", text))  # <Match object> — entire string

All three return a Match object on success or None on failure. Always check for None before calling .group().

Character Classes and Quantifiers

import re

# Character classes
re.findall(r"[aeiou]", "Hello World")     # ['e', 'o', 'o'] — vowels
re.findall(r"[0-9]+", "abc123def456")     # ['123', '456'] — digit sequences
re.findall(r"[A-Z][a-z]+", "Hello World") # ['Hello', 'World'] — capitalized words

# Predefined classes
# \d — digits       \D — non-digits
# \w — word chars   \W — non-word chars
# \s — whitespace   \S — non-whitespace
# .  — any char except newline

# Quantifiers
# * — zero or more    + — one or more
# ? — zero or one     {n} — exactly n
# {n,m} — between n and m
re.findall(r"\d+", "a1b22c333")          # ['1', '22', '333']
re.findall(r"\w{4}", "cat dog bird fish") # ['bird', 'fish'] — exactly 4 word chars

Capture Groups

Parentheses () create capture groups that extract specific parts of the match:

import re

# Extract area code and number from a phone number
pattern = r"(\d{3})-(\d{3})-(\d{4})"
match = re.search(pattern, "Call 555-123-4567")

if match:
    print(match.group(0))  # 555-123-4567 (entire match)
    print(match.group(1))  # 555 (first group)
    print(match.group(2))  # 123 (second group)
    print(match.group(3))  # 4567 (third group)
    print(match.groups())  # ('555', '123', '4567')

# Named groups
pattern = r"(?P<area>\d{3})-(?P<number>\d{3}-\d{4})"
match = re.search(pattern, "555-123-4567")
print(match.group("area"))    # 555
print(match.group("number"))  # 123-4567

Named groups ((?P<name>...)) make complex patterns much more readable — you access parts by name instead of index.

re.findall and re.finditer

import re

# findall — return all matches as a list
emails = re.findall(r"[\w.]+@[\w.]+", "Contact ada@x.com or bob@y.org")
print(emails)  # ['ada@x.com', 'bob@y.org']

# With groups, findall returns tuples
pairs = re.findall(r"(\w+)=(\d+)", "x=1, y=2, z=3")
print(pairs)  # [('x', '1'), ('y', '2'), ('z', '3')]

# finditer — return match objects (for position info)
for m in re.finditer(r"\d+", "a1b22c333"):
    print(f"Found {m.group()} at position {m.start()}")

re.sub — Substitution

import re

# Replace all occurrences
result = re.sub(r"\d+", "#", "abc123def456")
print(result)  # abc#def#

# Replace with a function
def double_match(match):
    return str(int(match.group()) * 2)

result = re.sub(r"\d+", double_match, "abc123def456")
print(result)  # abc246def912

# Limit number of replacements
result = re.sub(r"\d", "X", "a1b2c3d4", count=2)
print(result)  # aXbXc3d4

The function form of re.sub is powerful — it lets you transform each match dynamically.

Anchors

import re

# ^ — start of string    $ — end of string
# \b — word boundary

re.findall(r"^Hello", "Hello World")   # ['Hello'] — at start
re.findall(r"World$", "Hello World")   # ['World'] — at end
re.findall(r"\bcat\b", "cat catalog catch")  # ['cat'] — whole word only

Compiling Patterns

For patterns used repeatedly, compile them once for better performance:

import re

# Compile once, use many times
email_pattern = re.compile(r"^[\w.]+@[\w.]+\.\w+$")

# Now use the compiled pattern
print(email_pattern.match("ada@example.com"))  # Match
print(email_pattern.match("not-an-email"))     # None

# Compiled patterns have all the same methods
emails = email_pattern.findall("ada@x.com bob@y.org")

Compiling is faster when you use the same pattern in a loop or many times — the regex engine does not need to re-parse the pattern each time.

Flags

import re

# re.IGNORECASE — case-insensitive
re.findall(r"hello", "Hello HELLO hello", re.IGNORECASE)  # ['Hello', 'HELLO', 'hello']

# re.MULTILINE — ^ and $ match at line boundaries
re.findall(r"^\w+", "line1\nline2\nline3", re.MULTILINE)  # ['line1', 'line2', 'line3']

# re.DOTALL — . matches newlines too
re.findall(r".+", "line1\nline2", re.DOTALL)  # ['line1\nline2']

Combine flags with |: re.IGNORECASE | re.MULTILINE.

Example

              import re

# Basic search
match = re.search(r"\d{3}-\d{4}", "Call 555-1234 for help")
if match:
    print(f"Found phone: {match.group()} at position {match.start()}")

# Character classes and quantifiers
vowels = re.findall(r"[aeiou]", "Hello World")
print(f"Vowels: {vowels}")

numbers = re.findall(r"\d+", "abc123def456ghi789")
print(f"Numbers: {numbers}")

# Capture groups — extract parts
phone_pattern = r"(\d{3})-(\d{3})-(\d{4})"
match = re.search(phone_pattern, "Phone: 555-123-4567")
if match:
    area, exchange, number = match.groups()
    print(f"Area: {area}, Exchange: {exchange}, Number: {number}")

# Named groups
date_pattern = r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})"
match = re.search(date_pattern, "Date: 2024-01-15")
if match:
    print(f"Year: {match.group('year')}, Month: {match.group('month')}")

# Email extraction
text = "Contact ada@example.com or bob@test.org for info"
emails = re.findall(r"[\w.]+@[\w.]+\.\w+", text)
print(f"Emails: {emails}")

# Substitution
redacted = re.sub(r"\d{4}", "****", "Card: 1234-5678-9012-3456")
print(f"Redacted: {redacted}")

# Substitution with a function
def censor(match):
    word = match.group()
    return word[0] + "*" * (len(word) - 1)

censored = re.sub(r"\b\w{4}\b", censor, "some bad word here")
print(f"Censored: {censored}")

# Compiled pattern for reuse
email_re = re.compile(r"^[\w.]+@[\w.]+\.\w+$")
test_emails = ["ada@x.com", "invalid", "bob@y.org"]
for email in test_emails:
    valid = bool(email_re.match(email))
    print(f"  {email}: {'valid' if valid else 'invalid'}")

# Flags
case_insensitive = re.findall(r"python", "Python PYTHON python", re.IGNORECASE)
print(f"Case-insensitive matches: {case_insensitive}")
            

Shows re.search, character classes, capture groups (numbered and named), email extraction, substitution (simple and with a function), compiled patterns for validation, and the IGNORECASE flag.

Try it

  • Regex Tester

    Test regex patterns live — Python's re module uses the same syntax.

  • Text Counter

    Regex is perfect for counting specific patterns in text.

Common mistakes

The mistake

Not using raw strings (r'...') for regex patterns, causing backslash escape issues

The fix

Always prefix regex patterns with r: re.search(r'\d+', text). Without the r, Python interprets \d as an escape sequence before the regex engine sees it. Raw strings pass backslashes literally to the regex engine.

The mistake

Calling .group() on a match result without checking if it is None first

The fix

re.search and re.match return None if no match is found. Calling .group() on None raises AttributeError. Always check: 'match = re.search(...); if match: print(match.group())'. Or use re.findall which always returns a list.

Related Cheatsheets

Practice Regular Expressions

1 exercise

Practice Regular Expressions

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors