DevToolPal
EN

PCRE Regex Tester

Test PHP / Perl-compatible regular expressions with the real PCRE2 engine: recursion, possessive quantifiers, \K and more.

Runs locally, never uploaded

Options

Test string

0 chars · 0 B

Result

Result appears here

What this tool does

The PCRE regex tester checks a regular expression against your text using the real PCRE2 library, the same engine PHP, Perl-style tools and many web servers use. Matches are highlighted, each match is listed with its position and capture groups, and an optional replacement shows the result of a preg_replace-style substitution.

Testing against the real engine matters because PCRE and JavaScript regular expressions differ in small but important ways. A pattern that works in a browser-based tester can fail to compile in PHP, or match something different.

How to use it

  1. Type the pattern without delimiters in PCRE pattern.
  2. Paste the text to search into Test string.
  3. Turn on the modifiers you need: g for all matches (like preg_match_all), i, m, s, x, U, and u for Unicode-aware \w, \d and \b.
  4. Optionally fill in Replace with to preview the substitution. Use $1 or ${1} for numbered groups, ${name} for named groups and $0 for the whole match.

Invalid patterns show PCRE2’s own error message and the position in the pattern where it gave up.

Example

The default pattern \((?:[^()]++|(?R))*\) finds balanced parentheses. It matches a pair of brackets containing either non-bracket characters or, recursively, the whole pattern again. On the sample text it finds (a, g(b, c)) and ((x), y) and ignores the unclosed bracket at the end. JavaScript regular expressions cannot express this.

Try (?<amount>\d+(?:\.\d+)?)\s+(?<cur>USD|EUR) with the replacement ${cur} $1 to swap amounts and currency codes on the second line.

PCRE features worth knowing

  • Possessive quantifiers (*+, ++, ?+) and atomic groups ((?>...)) never give back what they matched, which prevents runaway backtracking.
  • \K resets the start of the match, a simple alternative to lookbehind.
  • Recursion with (?R), (?1) or (?&name) matches nested structures.
  • Inline modifiers such as (?i) change behaviour for part of a pattern.

Tips

  • In PHP, wrap the pattern in delimiters such as /.../ or ~...~ and pick one that does not appear in the pattern, so you do not have to escape it.
  • Turn on u when your text contains accented letters or CJK characters so \w matches them.

FAQ

› How is this different from the JavaScript regex tester?

This page runs PCRE2, the engine behind PHP's preg_ functions, Nginx, Apache and many other tools, compiled to WebAssembly. It supports syntax that JavaScript does not, such as recursion with (?R), possessive quantifiers like a++, atomic groups, \K, (?P<name>...) groups and the x and U modifiers.

› Do I type the delimiters, like /pattern/i?

No. Enter only the pattern and switch modifiers on with the checkboxes. In PHP, `preg_match('/\d+/i', $s)` corresponds to the pattern `\d+` with Ignore case turned on.

› Why did I get "match limit exceeded"?

Some patterns, such as ^(a+)+$ on a long non-matching string, backtrack an exponential number of times. The tester stops after five million steps so the page stays responsive. PHP has a similar safeguard, pcre.backtrack_limit, and preg_match returns false when it is hit.

› Is my text uploaded?

No. The PCRE2 WebAssembly module is downloaded once and everything runs in your browser.