Regular Expression Fundamentals
Match character classes, set boundary anchors, and scan raw string patterns with re.search and re.match.
Pattern Matching with Python's re Module
A regular expression is like a fingerprint scanner: you define a template, and it instantly searches text for matching prints. Python's built-in re module provides raw string notation, core search primitives, character classes, quantifiers, precompilation, and the Match object API.
Python strings process escape sequences like \n. In regex, backslashes denote special classes like \d. Without raw strings, you'd need to double-escape every backslash — always use r"..." for regular expressions.
search/match/fullmatch are three different levels of security checkpoint — search waves you through if the item shows up anywhere in your bag, match checks only what's on top, and fullmatch inspects every single item in the bag against the manifest.
Comparing re.search, re.match, and re.fullmatch
Run the snippet below to see how scope affects the match result:
import re
log_entry = "2026-09-17 ERROR [auth]: invalid token"
# 1. re.search scans anywhere in the string
err_match = re.search(r"ERROR", log_entry)
print("Search found:", err_match.group() if err_match else None)
# 2. re.match only checks the start (fails on ERROR!)
print("Match at start (ERROR):", re.match(r"ERROR", log_entry))
# But succeeds when matching the starting date:
print("Match at start (date):", bool(re.match(r"\d{4}-\d{2}-\d{2}", log_entry)))
# 3. re.fullmatch verifies complete strings (e.g. validating zipcodes)
print("Fullmatch 94103:", bool(re.fullmatch(r"\d{5}", "94103")))
print("Fullmatch 94103-ext:", bool(re.fullmatch(r"\d{5}", "94103-1234")))Finds the first occurrence of a pattern anywhere within the target string.
Checks only at index 0 — the pattern must begin at the very start of the string.
Precompiles a pattern object once for fast, repeated reuse in loops.
Returns the (start, end) index tuple, matching directly with string slicing.
Trace How Scope Changes the Match Result
Click through each function to see exactly what it matches:
search() scans the entire string for the first occurrence of the pattern, regardless of position.
Search Primitives, Character Classes, and the Match API
These three families cover the essential toolkit for pattern matching with the re module.
Core search primitives
re.search(pattern, string)Scans the entire string; returns the first Match or None.
re.match(pattern, string)Checks only at index 0 (start of string).
re.fullmatch(pattern, string)Requires the entire string to match exactly.
Character classes & quantifiers
\d \w \s / \D \W \SDigit, word, whitespace shorthand classes and their negations.
[abc] / [^abc]Character set / negated character set.
* + ? {m,n}Greedy quantifiers; append ? for lazy, minimal matching (*?, +?).
Compilation & Match API
re.compile(pattern)Precompiles a pattern once for fast, repeated reuse in loops.
match.group([n]) / .span()Returns matched text and the (start, end) index tuple.
Worked Hands-on Walkthroughs
Progress step-by-step from comparing search functions through character-class filtering to precompiled pattern inspection:
Comparing re.search, re.match, and re.fullmatch
search finds occurrences anywhere; match requires start-anchoring; fullmatch enforces exact string validation.
import re
log_entry = "2026-09-17 ERROR [auth]: invalid token"
# 1. re.search scans anywhere in the string
err_match = re.search(r"ERROR", log_entry)
print("Search found:", err_match.group() if err_match else None)
# 2. re.match only checks the start (fails on ERROR!)
print("Match at start (ERROR):", re.match(r"ERROR", log_entry))
# But succeeds when matching the starting date:
print("Match at start (date):", bool(re.match(r"\d{4}-\d{2}-\d{2}", log_entry)))
# 3. re.fullmatch verifies complete strings (e.g. validating zipcodes)
print("Fullmatch 94103:", bool(re.fullmatch(r"\d{5}", "94103")))
print("Fullmatch 94103-ext:", bool(re.fullmatch(r"\d{5}", "94103-1234")))search finds occurrences anywhere; match requires start-anchoring; fullmatch enforces exact string validation.
Filtering Tokens with Character Classes
\w+ matches letters, numbers, and underscores, while \d+ matches continuous sequences of digits.
import re
text = "User ada_lovelace logged in from IP 192.168.1.50 at 08:30"
# Extract username: word characters
user_match = re.search(r"User (\w+)", text)
print("Username:", user_match.group(1))
# Extract IP address: digits separated by dots
ip_match = re.search(r"\d+\.\d+\.\d+\.\d+", text)
print("IP Address:", ip_match.group(0))
# Extract timestamp: hh:mm
time_match = re.search(r"\d{2}:\d{2}", text)
print("Timestamp:", time_match.group(0))\w+ matches letters, numbers, and underscores, while \d+ matches continuous sequences of digits.
Compiling Patterns and Inspecting Match Slices
Precompiled patterns offer maximum throughput, and match.span() correlates directly with Python string slicing.
import re
# Compile once upfront
UUID_PATTERN = re.compile(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", re.IGNORECASE)
candidates = [
"123e4567-e89b-12d3-a456-426614174000",
"INVALID-UUID-FORMAT",
"c9bf9e57-1685-4c89-bafb-ff5af830be8a"
]
valid_uuids = [c for c in candidates if UUID_PATTERN.fullmatch(c)]
print("Valid UUIDs:", valid_uuids)
# Inspecting Match bounds
text = "Status: OK (HTTP 200)"
status_code_pat = re.compile(r"\d{3}")
m = status_code_pat.search(text)
if m:
print(f"Found '{m.group()}' at span {m.span()} in '{text}'")
print(f"Verified slice: text[{m.start()}:{m.end()}] -> '{text[m.start():m.end()]}' ")Precompiled patterns offer maximum throughput, and match.span() correlates directly with Python string slicing.
Common Traps & How to Avoid Them
Study the mistakes and clean fixes below before relying on regex in your own code:
# Mistyped variable or missing closing parenthesis
result = regular_expressions__pattern_matching_val# Declare and verify variables before execution
val = 10
print("Defined correctly:", val)Beginners frequently encounter typos or forgotten definitions before using an identifier.
# Modifying shared state or unexpected variable shadowing
def update_state(data=[]):
data.append(1)
return datadef update_state(data=None):
if data is None:
data = []
data.append(1)
return dataDefault arguments in Python are evaluated once at function definition time, not at invocation time.
# Opening resources in a tight loop without closing or bounding memory
files = [open(f"log_{i}.txt", "w") for i in range(5000)]for i in range(5):
with open(f"log_{i}.txt", "w") as f:
f.write("Safe")Failing to release system resources leads to process termination under heavy production workloads.
Real-World Systems in Action
See how scripts, backend services, and large-scale security systems all lean on regex for text processing at scale:
Local Developer Automation Tool
Individual developers and IT administrators automate everyday repetitive workflows.
A clean standalone script providing fast, local automation with zero dependencies.
import re
text = "Order ID: 98452 confirmed"
match = re.search(r"\d+", text)
if match:
print("Found ID:", match.group())Backend API Service Integration
SaaS platforms run backend microservices processing requests concurrently.
A modular service handler integrating validation and structured responses.
# Service integration layer
def handle_request(payload: dict) -> dict:
return {"status": "success", "processed": True}High-Throughput Cloud Architecture
Web application firewalls (WAFs) and security scanners evaluate incoming HTTP requests against regex patterns to block injection attacks.
Web application firewalls (WAFs) and security scanners evaluate incoming HTTP requests against regex patterns to block injection attacks.
# High-resilience production pattern
import time
def process_stream(events: list) -> int:
processed = 0
for event in events:
if event:
processed += 1
return processedKey Points & Concept Review
- Always use raw strings r'...' for regular expressions to prevent escape sequence corruption.
- re.search() scans the entire string; re.match() checks only the start; re.fullmatch() checks the entire string.
- Character classes (\d, \w, \s) match common token categories.
- Standard quantifiers (*, +) are greedy; append ? (e.g. *?, +?) for minimal lazy matching.
- re.compile() pre-compiles regex patterns for high-performance reuse in loops.
- Match objects provide span(), start(), and end() to pinpoint exact string slice boundaries.
Frequently Asked Questions & Reference Hub
A regex that works on your one example string is not the same as a regex that works in production. Test against empty strings, unexpected whitespace, and malformed input before trusting a pattern with real user data.
The official standard library reference for regex operations.
Understand why r'...' notation prevents escape sequence bugs.
See side-by-side comparisons of the three scoping behaviors.
Learn when to use *? and +? to avoid overmatching.
See benchmarks showing precompiled pattern reuse gains.
Test and visualize your patterns against sample text live.
You've Mastered Regular Expression Fundamentals!
You understand raw strings, the search/match/fullmatch scoping differences, character classes, quantifiers, and precompilation. Now lock in your knowledge with hands-on practice.
Extract a phone number from text using re.search() and verify the output.
Earn experience points toward your developer rank as soon as you pass.
Bite-sized knowledge check with instant explanations for every question.
Worked example
Search for a digit sequence in a log line.
import re m = re.search(r"\d+", "Score 95") print(m.group())
How it works, step by step
- re.search() returns a match object containing matched text.
Regular Expression Fundamentals practice
Given a message text = "Contact support at 555-0199 for help.", use re.search() with pattern r"\d{3}-\d{4}" to find the phone number and print: Phone: 555-0199.
Coding Practice is Locked
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.
Practice quiz
Check yourself
15 questions with explanations after submission, so you can correct course as you go.
Regular Expression Fundamentals quiz
LockedYou must be signed in to take this quiz. Sign in to test your understanding, track your answers, and earn XP.
Lesson incomplete
Not completedCreate an ID or sign in to save lesson completion across devices.