RegEx Editor PHP 8.5.12 · PCRE 10.40

Regex reference (PCRE)

The main building blocks of the syntax PHP uses for regular expressions. “Try it” opens the tester with an example.

In PHP the pattern sits between delimiters, for example '/(\d+)/i'. This tester picks the delimiter for you.

Characters and classes

TokenMeaningExampleTry it
. Any character except a newline Z.mmer Try it .
\d A digit 0 to 9 \d+ Try it \d
\D Anything but a digit \D+ Try it \D
\w Word character: letter, digit, underscore \w+ Try it \w
\W Anything but a word character \W+ Try it \W
\s Whitespace (space, tab, newline) \s Try it \s
\S Anything but whitespace \S+ Try it \S
[abc] One of the characters a, b or c [abc] Try it [abc]
[^abc] One character except a, b, c [^abc] Try it [^abc]
[a-z] One character from a to z [a-z]+ Try it [a-z]
\p{L} Any Unicode letter (with modifier u) \p{L}+ /u Try it \p{L}
\R Any line break (\n, \r\n, \r) \R Try it \R
\h Horizontal whitespace (space, tab) \h+ Try it \h

Quantifiers

TokenMeaningExampleTry it
* Zero or more, as many as possible a* Try it *
+ One or more, as many as possible a+ Try it +
? Zero or one colou?r Try it ?
{n} Exactly n times \d{4} Try it {n}
{n,m} Between n and m times \d{1,2} Try it {n,m}
*? +? Lazy: as few as possible <.+?> Try it *? +?
*+ ++ Possessive: no giving back (faster, no backtracking) a++b Try it *+ ++

Anchors and boundaries

TokenMeaningExampleTry it
^ Start of the subject (with m: of every line) ^\w+ /m Try it ^
$ End of the subject (with m: of every line) \w+$ /m Try it $
\A Only at the very start \A\w+ Try it \A
\z Only at the very end \w+\z Try it \z
\b Word boundary \bist\b Try it \b
\B Not a word boundary \Bist Try it \B

Groups and back-references

TokenMeaningExampleTry it
( ) Group that remembers its match (\d+)-(\d+) Try it ( )
(?: ) Group without remembering (?:ab)+ Try it (?: )
(?<name> ) Named group (?<jahr>\d{4}) Try it (?<name> )
\1 Back-reference to group 1 (\w)\1 Try it \1
\k<name> Back-reference to a named group (?<z>\w)\k<z> Try it \k<name>
a|b Either a or b Hund|Katze Try it a|b

Lookaround

TokenMeaningExampleTry it
(?= ) Lookahead: followed by … \d+(?= EUR) Try it (?= )
(?! ) Negative lookahead: not followed by … \d+(?! EUR)\b Try it (?! )
(?<= ) Lookbehind: preceded by … (?<=EUR )\d+ Try it (?<= )
(?<! ) Negative lookbehind: not preceded by … (?<!EUR )\b\d+ Try it (?<! )

Modifiers

TokenMeaningExampleTry it
i Ignore case php /i Try it i
m ^ and $ match per line ^\w+ /m Try it m
s Dot also matches newline a.b /s Try it s
x Whitespace and comments allowed in the pattern \d+ # Zahl /x Try it x
u UTF-8: characters instead of bytes, \p{L} usable ^.$ /u Try it u
U Quantifiers lazy by default a+ /U Try it U
D $ only at the very end a$ /D Try it D
A Pattern anchored at the start a /A Try it A
n No automatic groups (named only) (a)(?<x>b) /n Try it n

PHP functions

TokenMeaningExample
preg_matchFirst match and its groupspreg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
preg_match_allAll matches; with PREG_SET_ORDER one array per matchpreg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
preg_replaceReplace matchespreg_replace($pattern, $replacement, $subject);
preg_splitSplit the subject at matchespreg_split($pattern, $subject, -1, PREG_SPLIT_NO_EMPTY);
preg_quoteEscape special characters for a patternpreg_quote($text, '/');