Python Regex Tester

Test regular expressions written for Python (re module) — with warnings for anything that works differently there, and Python code to copy.

//g

Python regex syntax at a glance

Literal
re.compile(r"pattern") — use a raw string so backslashes stay as written
Named group
(?P<name>…), back-reference (?P=name)
Replacement
\1, \g<1>, \g<name>
Flags
re.IGNORECASE, re.MULTILINE, re.DOTALL, re.VERBOSE, re.ASCII — or (?i) at the very start
Find all
re.findall(), re.finditer()

What's different in Python

  • →re.match() only matches at the start of the string; re.search() looks anywhere and re.fullmatch() needs the whole string.
  • →Lookbehind must be fixed-width: no *, +, ? or {n,m} inside (?<=…).
  • →Atomic groups (?>…) and possessive quantifiers (*+, ++) arrived in Python 3.11.
  • →\d and \w match Unicode digits and letters by default; add re.ASCII to limit them to [0-9] and [A-Za-z0-9_].
  • →The re module has no \p{…} Unicode properties; the third-party regex package adds them.
  • →Without re.MULTILINE, $ matches at the end and also just before a final newline; \Z is the absolute end.

Frequently asked

Why does re.match() return None when the text contains my pattern?

re.match() is anchored at the start of the string. Use re.search() to find the pattern anywhere, or re.fullmatch() to require the whole string to match.

Why should I use r"…" strings for Python regexes?

In a normal string, \d or \b are processed by Python first (\b becomes a backspace character). A raw string passes the backslashes to the regex engine untouched.

Does this tester run Python's re module?

Matches run in your browser's JavaScript engine. Python-only syntax with an exact equivalent — like (?P<name>…), (?P=name), (?i) and \Z — is translated before testing, and anything that behaves differently in Python is flagged above the results.

ToolNinja