PyCodeItPython trace & execution models
Python Mastery Hub/regular expressions
Python 3.12+ CPython TrackPyodide WASM Sandbox

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.

Regex Compilation, Backtracking Limits, and Match Object Inspection

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.

Catastrophic Backtracking and Escape Character Stripping

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

Problem 1 / 43+50 XP

Greedy Matching Trap in HTML Tags

hard
Task: Output Execution Prediction

Trace CPython execution step-by-step and deduce the exact stdout string emitted by this snippet.

Socratic Guidance-5 XP penalty per revealed hint
Topic Problems0 / 43 Solved
main.pyCPython 3.12 (WASM)
1
2
3
import re
text = '<b>bold</b> and <i>italic</i>'
print(re.findall(r'<.*>', text))
Press Enter to submit
Match exact whitespace and capitalization

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.