Regular Expressions & Pattern Matching
Master greedy vs non-greedy quantifiers, zero-width assertions, capturing groups, re.sub callbacks, and Unicode edge cases.
How Regular Expressions & Pattern Matching behaves during execution
Python's re module provides regular expression pattern matching implemented in C. Regular expressions compile patterns into deterministic or non-deterministic state machines that match character classes, boundaries, groups, and lookarounds across text. Understanding greedy versus non-greedy quantifiers, raw string escape mechanics, and pre-compiled regex objects is critical for high-throughput string parsing.
When evaluating complex patterns with nested quantifiers, backtracking engines explore candidate paths sequentially. Re-compiling regular expressions inside loops with re.search(pattern, text) causes redundant compilation overhead; patterns should be compiled once using re.compile() and reused. Match objects expose span(), group(), and groupdict() to inspect captured substrings without re-scanning.
A severe production hazard is catastrophic backtracking: patterns with overlapping nested quantifiers (like (a+)+$) take exponential time on non-matching inputs, freezing application threads (ReDoS). Another frequent beginner bug is omitting the raw string prefix r'...' in patterns, which causes Python's string parser to consume backslashes before the regex engine ever sees them.
How to use this tool
Read the server-rendered examples above first, write down each variable change in a trace table, then use the interactive workspace below to step through execution. Tracing builds clear mental models for technical interviews.
Worked example
# Trace the execution line by line x = 10 y = x * 2 print(x + y)
Static result: 30
Controls
The code panel shows the snippet; Run executes it in the browser Pyodide sandbox; Next and Previous step through line states; the result panel displays output and state changes.
Honest limits
Python code executes locally inside your browser WebAssembly (Pyodide) sandbox. Execution is client-side, rate-free, and isolated from our servers. Do not enter credentials or private API keys.
Continue with the Python Mastery Hub.
Interactive Dry-Run Execution Workspace
Greedy Matching Trap in HTML Tags
Trace CPython execution step-by-step and deduce the exact stdout string emitted by this snippet.
import re
text = '<b>bold</b> and <i>italic</i>'
print(re.findall(r'<.*>', text))Frequently Asked Questions on Regular Expressions & Pattern Matching
Why should regex patterns in Python always be written as raw strings (r'...)?
Without raw string syntax (r'...'), Python's standard string literal interpreter processes backslashes first. For example, '\b' becomes an ASCII backspace (0x08) instead of a regex word boundary. Raw strings pass backslashes untouched to the regex compiler.
What causes catastrophic backtracking in regular expressions?
Catastrophic backtracking happens when a regex engine evaluates overlapping greedy quantifiers (such as (x+)+y) against an input that almost matches but fails at the end. The engine tries an exponential number of branch combinations, exhausting CPU resources.
What is the difference between re.match() and re.search() in Python?
re.match() only looks for matches anchored at the very beginning of the string (index 0). In contrast, re.search() scans the entire string from left to right and returns the first match found anywhere.