Python Regex Tester
Test regular expressions with Python's own re module.
Python regex cheat sheet
| Pattern | Meaning | Example |
|---|---|---|
| . | Any character except a newline | h.t matches hat, hot |
| \d \w \s | A digit, a word character, a space | \d+ matches 2026 |
| \D \W \S | Anything that is not a digit, word character, space | \S+ matches one word |
| [abc] [^abc] [a-z] | One of these, none of these, a range | [aeiou] matches a vowel |
| * + ? | Zero or more, one or more, optional | colou?r matches color, colour |
| {3} {2,4} | Exactly 3, between 2 and 4 | \d{4} matches a year |
| ^ $ | Start and end of the text (each line with MULTILINE) | ^\d+$ matches a whole number |
| \b | A word boundary | \bcat\b skips "category" |
| (...) | A group you can read back | (\d+)-(\d+) |
| (?P<name>...) | A named group (Python spelling) | (?P<year>\d{4}) |
| (?:...) | A group that is not captured | (?:Mr|Ms)\. ?\w+ |
| a|b | Either side | cat|dog |
| (?=...) (?!...) | Followed by, not followed by | \w+(?=ing) |
| (?<=...) (?<!...) | Preceded by, not preceded by (fixed width) | (?<=\$)\d+ |
| *? +? | As few as possible (lazy) | <.+?> matches one tag |
More in the Python regex tutorial.
Questions
Is this the same as Python regex?
Yes. Matches come from Python's own re module, so named groups, lookbehinds and flags behave as they do in your program.
How is Python regex different from JavaScript regex?
Python writes named groups as (?P<name>...) and refers to them as \g<name> in re.sub, has no g flag (use re.findall or re.finditer for every match), and uses flags such as re.IGNORECASE instead of letters after the pattern. Lookbehinds must have a fixed width.
What is the difference between re.match, re.search and re.findall?
re.match only looks at the start of the text, re.search finds the first match anywhere, re.findall returns every match as a list, and re.finditer gives match objects with positions and groups, which is what this tester shows.
Why write the pattern as r"..."?
A raw string keeps backslashes as they are. Without the r, Python reads "\b" as a backspace character before the regex engine ever sees it. The code this page generates always uses a raw string.