PHP Reference Β· Regular Expressions
PHP regular expression (PCRE) syntax
Reference for PCRE regular expression syntax in PHP: delimiters, character classes, quantifiers, anchors, groups, alternation, backreferences and lookarounds.
PHP's preg_* functions use PCRE, a regular expression dialect that is close to the one in Perl and many other languages. A pattern is a string made of three parts: an opening delimiter, the expression itself and a closing delimiter, followed by optional modifiers.
<?php $pattern = '/^colou?r$/i'; // delimiter / ... / and the modifier i var_dump(preg_match($pattern, 'Color'));
Write patterns in single quotes. In double quotes PHP expands $name and \n before the regex engine sees them, and you would need to double every backslash.
Delimiters
Any character that is not a letter, digit, backslash or whitespace can be the delimiter. Slash is the usual choice; use #, ~ or % when the pattern is full of slashes. Bracket pairs (), [], {} and <> work as opening and closing delimiters. If the delimiter appears inside the pattern, escape it with a backslash, or let preg_quote() do it for variable text.
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Characters, classes and quantifiers
| Syntax | Meaning |
|---|---|
. |
Any character except a newline (any character with the s modifier) |
\d \D |
A digit / a non-digit |
\w \W |
A word character [A-Za-z0-9_] / a non-word character |
\s \S |
A whitespace character / a non-whitespace character |
\h \R |
A horizontal whitespace character / any line break (\r\n, \n, \r) |
[abc] [a-f0-9] |
One character from the set or range |
[^abc] |
One character not in the set |
[[:alpha:]] |
POSIX class: alpha, digit, alnum, upper, lower, space, punct |
\p{L} \p{Lu} \p{Han} |
A Unicode letter / uppercase letter / script (needs /u) |
* + ? |
0 or more, 1 or more, 0 or 1 |
{n,} {n,m} |
Exactly n, at least n, between n and m |
*? +? ?? {n,m}? |
Lazy versions: match as little as possible |
*+ ++ ?+ |
Possessive versions: never give characters back |
Quantifiers are greedy by default. In <.+> applied to <b>bold</b> the match runs from the first < to the last >, while <.+?> stops at the first >.
Anchors and boundaries
| Syntax | Meaning |
|---|---|
^ $ |
Start and end of the subject (of each line with m); $ also matches before a final newline |
\A \z \Z |
Start of subject / very end / end or before a final newline |
\b \B |
A word boundary / not a word boundary |
\G |
Where the previous match ended |
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Groups, alternation and backreferences
| Syntax | Meaning |
|---|---|
(β¦) |
Capturing group, numbered from the left by opening parenthesis |
(?:β¦) |
Non-capturing group |
(?<name>β¦) |
Named capturing group (also (?P<name>β¦) and (?'name'β¦)) |
a|b |
Alternation: a or b (the first alternative that works wins) |
\1 \k<name> |
Match the same text again as an earlier group |
(?i) (?i:β¦) |
Turn a modifier on for the rest of the group, or for the inner group only |
(?>β¦) |
Atomic group: no backtracking into it |
| `(? | β¦)` |
(?R) (?1) |
Recurse into the whole pattern / group 1 |
Lookarounds
A lookaround checks the text next to the current position without consuming it.
| Syntax | Meaning |
|---|---|
(?=β¦) |
Positive lookahead: what follows must match |
(?!β¦) |
Negative lookahead: what follows must not match |
(?<=β¦) |
Positive lookbehind: what precedes must match |
(?<!β¦) |
Negative lookbehind: what precedes must not match |
Lookbehind needs a bounded length ((?<=ab|abc) is fine, (?<=a+) is not).
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Usage notes
- Escape the special characters
. ^ $ * + ? ( ) [ ] { } | \with a backslash to match them literally. Inside a character class only\,],^and-need care. - Alternation order matters:
/a|ab/matchesainab, because the first alternative succeeds. Put longer alternatives first. - Avoid nested quantifiers such as
(a+)+on text that can fail to match. They cause catastrophic backtracking; seepreg_last_error(). Possessive quantifiers and atomic groups help. - Byte versus character. Without
/u,.and classes work on bytes. Add/ufor UTF-8 text. - Validate shape, then meaning.
/^\d{4}-\d{2}-\d{2}$/accepts2026-02-31; usecheckdate()afterwards. - Comments in patterns are possible with the
xmodifier or(?#comment). - Prefer simple functions such as
str_contains()andexplode()for fixed text.