Skip to content
dz / php-regex-tester
Text · Regex tester

PHP regex tester – preg_match online

PCRE2 is selected – the pattern runs through PHP's own preg_* functions, so the result is exactly what your code gets.

/regex

From tester to code

Write the pattern without delimiters – the tester picks one that does not clash with your pattern and shows the full PHP string under Code. Flags map to PHP modifiers: i, m, s, x, u (UTF-8), U (ungreedy), n (no auto-capture, PHP 8.2+). g switches between preg_match (first match) and preg_match_all.

preg_match_all('/^(?<key>\w+)\s*=\s*(?<value>.*?)\s*$/m', $ini, $m, PREG_SET_ORDER);

foreach ($m as $row) {
    echo $row['key'], ' → ', $row['value'], PHP_EOL;   // named groups appear by name and number
}

PHP pitfalls

  • Backslashes in strings – in single quotes, '\d' stays \d, but a literal backslash needs '\\\\'. The generated code escapes correctly.

  • UTF-8 – without u, PCRE works on bytes: . matches half an é and offsets are byte positions.

  • Errors return false – preg_match returns false on errors such as the backtracking limit; check preg_last_error_msg().

  • User input in a pattern – always pass it through preg_quote($s, '/').

Questions

Which PCRE version runs here?

The status line under the test string shows the exact PCRE2 and PHP version of each run.

Is my text stored?

No. PCRE2 runs on our server, so the pattern and text are sent there for each test, processed in memory and not logged.

Why does preg_replace not accept ${name}?

PHP replacements reference groups by number only – $1, ${1} or \1. Use preg_replace_callback to work with named groups.

▸ About this tool

About the Regex Tester

Introduction

Regular expressions are compact, powerful – and easy to get subtly wrong. This tester shows exactly what a pattern matches while you type it: every match highlighted in your text, each capture group with its value and position, the result of a replacement, a plain-English explanation of the pattern, and code you can paste straight into your project. Unlike most online testers, it does not imitate other languages in JavaScript: each flavor runs on its real engine.

Three real engines

  • JavaScript – the regex engine of your own browser (RegExp), with the g i m s u v y flags. It runs locally; nothing is sent anywhere.

  • PCRE2 · PHP – PHP's preg_match, preg_match_all and preg_replace on our server. PCRE2 is also the engine behind many other tools, so it is the best reference for PHP and closely related Perl-style syntax: possessive quantifiers a++, atomic groups, \A \z, (?P<name>…), the x, U and n modifiers.

  • .NET · C# – System.Text.RegularExpressions from .NET 10 on our server, with RegexOptions such as IgnoreCase, Singleline, ExplicitCapture and RightToLeft.

The differences are real: in .NET, named groups are numbered after all unnamed ones, so $2 in a replacement may mean something else than in JavaScript or PHP. Switch the flavor and compare.

How to use it

  1. Type a pattern between the slashes and toggle the flags below it. g finds all matches instead of the first.

  2. Paste the text to search. Matches light up in alternating colours; the Matches list shows each match and its groups – click one to select it in the text.

  3. Tick Replace to try a replacement with group references ($1, ${name}, $<name> depending on the flavor).

  4. Explanation describes the pattern token by token, Code gives a ready-made snippet for JavaScript, PHP or C#, and Reference is a cheat sheet – click a token to insert it.

Short links: develzone.org/regex, /regexp and /re. Pasting a regex literal such as /\d+/g into the search box on any page opens it here. Share copies a link with the pattern, flags and test text.

Catastrophic backtracking

Some patterns – typically nested quantifiers such as (a+)+$ – take exponentially long on text that almost matches. A server with such a regex can be brought down by one request (a "ReDoS"). The tester stops every run after about two seconds (JavaScript and .NET) or when PCRE's backtracking limit is reached, and tells you why. The fix is usually a more specific pattern, an atomic group (?>…) or a possessive quantifier a++ where the flavor supports them.

Good to know

  • Match positions are counted in UTF-16 code units, as JavaScript does – an emoji counts as two.

  • Use u in JavaScript and PCRE2 for correct Unicode handling and \p{L}-style classes.

  • Validating e-mail addresses or URLs with a regex is always a compromise – the Common patterns menu has practical versions with test text that shows where they stop.

  • Need to compare two texts instead? Try the text diff.

Privacy

The JavaScript flavor runs entirely in your browser. For PCRE2 and .NET, the pattern and test text are sent to our server for each test, processed in memory and not logged or stored. Do not paste secrets you would not want to leave your computer – use the JavaScript flavor for those.

❯ Esc
↑ ↓ select ↵ open Shift ↵ new line