Python Regex Tester for the re module
Write the pattern exactly as you would in a Python raw string, including (?P<name>…) groups, (?P=name) backreferences, inline flags and \A and \Z, and see the matches, groups and re.sub result live. Then copy re.compile code with the flags already set.
Testing JavaScript, Go or PHP instead? Use the general regex tester.
Click any pattern to load it into the tester
How to use the python regex tester
Matching runs in your browser as you type, so nothing you paste is uploaded.
Write the pattern as a raw string body
Type what goes between the quotes of
r'…'. You do not need to double backslashes:\d+here isr'\d+'in Python. Named groups use Python syntax,(?P<year>\d{4}).Set the flags
iisre.IGNORECASE,misre.MULTILINE(^and$at every line) andsisre.DOTALL(.matches a newline). Inline flags such as(?i)at the start of the pattern work too.glists every match, likere.finditer.Paste real input
Use an actual log line or file excerpt rather than a tidy sample. Most regex bugs are a trailing space, a tab, or a field that is sometimes empty, and they only show up on real text.
Check the groups
The Groups tab shows what each group captured, including named groups by name, which is what
m.group('year')andm.groupdict()return.Preview re.sub
In the Replace tab, write the replacement the Python way:
\1or\g<1>for numbered groups and\g<name>for named ones. The preview shows the result before you run it on a file.Copy the code
The Code Snippet tab produces
re.compileandre.subcode with a correctly quoted raw string and the flags combined with|. Switch the language to get the same pattern for JavaScript, Go, Java or PHP.
Where Python's re differs from the browser engine
Matching here runs on the browser's JavaScript engine. Python syntax is translated first, so named groups, \A, \Z and inline flags behave as they do in Python. These are the places where the two still differ, and the tester warns you when your pattern hits one.
$ and a trailing newline
In Python, $ matches at the very end of the text and also just before a final newline.
So re.search(r'end$', 'the end\n') matches in Python but not here. Use \Z when you mean the absolute end, or add \n? before $ to preview the Python behaviour.
re.match versus re.search
re.match only matches at the start of the string; re.search scans the whole string. This tester behaves like re.search and re.finditer.
If your code uses re.match, add \A (or ^) to the start of the pattern here to see what it will actually match. re.fullmatch needs both \A and \Z.
Possessive quantifiers and atomic groups
Python 3.11 added *+, ++, ?+ and (?>…).
The browser engine has neither, so they are previewed as ordinary greedy quantifiers and the tester flags it. Results can differ when backtracking matters.
Verbose mode
re.VERBOSE or (?x) lets you spread a pattern over lines with comments.
Verbose mode is not previewed, so whitespace and # comments are matched literally. Test the compact form here, then add the formatting in your code.
Unicode by default
In Python 3, \w, \d and \s match Unicode letters, digits and spaces unless you pass re.ASCII.
The browser treats them as ASCII, so \w here will not match é or ß but Python will. For ASCII-only input this makes no difference.
Questions people ask
How do I test a Python regex online?
Type the pattern as it appears inside your r'…' string, set the flags, and paste your text. Matches and capture groups update as you type, and the Code Snippet tab gives you re.compile or re.sub code ready to paste.
Does this run real Python?
No. Matching runs on your browser's JavaScript engine after translating Python-only syntax such as (?P<name>…), (?P=name), \A and \Z. The tester lists anything that cannot be reproduced exactly, such as possessive quantifiers, verbose mode, and $ before a trailing newline.
What is the difference between re.match, re.search and re.findall?
re.match checks only at the start of the string, re.search finds the first match anywhere, and re.findall returns every match. re.finditer is like findall but yields match objects with positions and groups, which is usually what you want.
How do I use named groups in Python regex?
Write (?P<name>pattern) to name a group, (?P=name) to match the same text again, and \g<name> in a re.sub replacement. In code, m.group('name') or m.groupdict() returns what it captured.
Why use a raw string for Python regex?
Without the r prefix, Python processes backslash escapes before the regex engine sees the pattern, so \b becomes a backspace character. A raw string passes backslashes through unchanged, which is why regex patterns should always be written as r'…'.