Python Regex Tester & Reference
Test regular expressions for Python's re module. Paste your pattern and test string to see matches instantly, then copy the Python code examples into your project. All testing happens in your browser using JavaScript regex (syntax is nearly identical to Python).
Regex Tester
Test regular expressions in real time with match highlighting and capture groups.
Matches (0)
Enter a pattern and test string to see matches
How to use regex in Python
Python's re module: import re. Key functions: re.search(pattern, string) returns the first match or None. re.match(pattern, string) matches only at the start. re.findall(pattern, string) returns all matches as a list. re.sub(pattern, replacement, string) replaces matches. re.compile(pattern) creates a reusable pattern object. Always use raw strings for patterns: re.search(r'\d+', text) — the r prefix prevents Python from interpreting backslashes before the regex engine sees them.
# Python — regular expressions with re module
import re
text = "Contact us at support@example.com or sales@example.com"
# Find all email addresses
emails = re.findall(r"[\w.+-]+@[\w-]+\.[\w.]+", text)
print(emails) # ["support@example.com", "sales@example.com"]
# Search for first match
match = re.search(r"(\w+)@(\w+\.\w+)", text)
print(match.group(1)) # "support"
print(match.group(2)) # "example.com"
# Replace with regex
cleaned = re.sub(r"[\w.+-]+@[\w-]+\.[\w.]+", "[REDACTED]", text)
print(cleaned) # "Contact us at [REDACTED] or [REDACTED]"
# Compile for reuse
pattern = re.compile(r"^\d{3}-\d{4}$")
print(pattern.match("555-1234")) # Match objectPython regex vs JavaScript regex differences
Most patterns work identically in Python and JavaScript. Key differences: Python supports named groups with (?P<name>...) while JavaScript uses (?<name>...) — both syntaxes work in Python 3.6+. Python has re.VERBOSE (x flag) for commented patterns. Python supports lookbehind assertions of variable length; JavaScript requires fixed-length. Python's re.split() keeps captured groups in the result; JavaScript's String.split() does the same. The \b, \d, \w, \s character classes work the same in both. Test your pattern here — if it works in this tester, it will work in Python with minimal changes.
Common Python regex patterns
Email: r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'. URL: r'https?://[^\s]+'. IP address: r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'. Phone: r'\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}'. Date (YYYY-MM-DD): r'\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])'. Python variable: r'[a-zA-Z_][a-zA-Z0-9_]*'. For production email validation, use a library — regex alone cannot fully validate RFC 5322 email addresses.
Frequently Asked Questions
What is the difference between re.match() and re.search()?
re.match() only checks for a match at the beginning of the string. re.search() scans the entire string for the first match. Use re.search() unless you specifically need to match from the start. For full-string matching, use re.fullmatch() or anchor your pattern with ^ and $.
Why should I use raw strings (r'') for Python regex?
Raw strings prevent Python from interpreting backslashes. Without r'', you'd need to double-escape: '\\d+' instead of r'\d+'. Since regex patterns use many backslashes, raw strings make them much more readable and less error-prone.
How do I use regex flags in Python?
Pass flags to re functions: re.search(pattern, text, re.IGNORECASE | re.MULTILINE). Common flags: re.IGNORECASE (re.I) for case-insensitive matching, re.MULTILINE (re.M) makes ^ and $ match line boundaries, re.DOTALL (re.S) makes . match newlines, re.VERBOSE (re.X) allows comments and whitespace in patterns.
Related Inspect Tools
TypeScript 6.0 Migration Checker
Analyze your tsconfig.json for TS 6.0 breaking changes, deprecated options, new defaults, and get a readiness grade with fixes
AI Code Security Scanner
Scan JavaScript and TypeScript code for vulnerabilities — hardcoded secrets, injection, XSS, SSRF, prototype pollution, and 20+ security checks
Code Complexity Analyzer
Analyze JavaScript and TypeScript code for cyclomatic complexity, cognitive complexity, nesting depth, and maintainability index with per-function metrics
GitHub Actions Validator
Validate GitHub Actions workflow YAML files for syntax, triggers, job structure, step config, needs dependencies, and deprecated actions