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
| Token | Meaning | Example | Try 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
| Token | Meaning | Example | Try 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
| Token | Meaning | Example | Try 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
Lookaround
| Token | Meaning | Example | Try 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
| Token | Meaning | Example | Try 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
| Token | Meaning | Example |
| preg_match | First match and its groups | preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE); |
| preg_match_all | All matches; with PREG_SET_ORDER one array per match | preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER); |
| preg_replace | Replace matches | preg_replace($pattern, $replacement, $subject); |
| preg_split | Split the subject at matches | preg_split($pattern, $subject, -1, PREG_SPLIT_NO_EMPTY); |
| preg_quote | Escape special characters for a pattern | preg_quote($text, '/'); |