PHP (PCRE) Regex Tester
Test regular expressions written for PHP (PCRE) (PCRE2 (preg_*)) — with warnings for anything that works differently there, and PHP (PCRE) code to copy.
//g
PHP (PCRE) regex syntax at a glance
- Literal
- '/pattern/flags' with delimiters — escape / inside, or use # or ~ as the delimiter
- Named group
- (?<name>…), (?P<name>…) or (?'name'…)
- Replacement
- $1, ${1}, \1 in preg_replace
- Flags
- i, m, s, x, u (UTF-8), D (dollar end only)
- Find all
- preg_match_all('/…/', $text, $matches)
What's different in PHP (PCRE)
- →PCRE is also what nginx, Apache, grep -P and many tools use, so this page fits their patterns too.
- →Add the u modifier for UTF-8 text, or multibyte characters are treated as separate bytes.
- →Recursion (?R), conditionals, possessive quantifiers and atomic groups are all supported.
- →Variable-length lookbehind needs PCRE2 10.43+ (PHP 8.4); older versions want fixed-length lookbehind.
Frequently asked
Why do I get "No ending delimiter '/' found"?
PHP regexes need delimiters: '/\d+/' not '\d+'. If the pattern contains /, escape it as \/ or pick another delimiter like #…#.
Does nginx use the same regex syntax?
Yes — nginx location and rewrite rules use PCRE, so named groups, lookarounds and the flags here behave the same. The case-insensitive form is written ~* in nginx.