DevBolt
Processed in your browser. Your data never leaves your device.

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).

← Back to tools

Regex Tester

Test regular expressions in real time with match highlighting and capture groups.

//g

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 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