Skip to content
Tutorials Center
My learning

Topics

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
<?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.

PHP Delimiters and escaping

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
PHP Greedy, lazy and anchors

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).

PHP Groups, backreferences and lookarounds

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/ matches a in ab, 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; see preg_last_error(). Possessive quantifiers and atomic groups help.
  • Byte versus character. Without /u, . and classes work on bytes. Add /u for UTF-8 text.
  • Validate shape, then meaning. /^\d{4}-\d{2}-\d{2}$/ accepts 2026-02-31; use checkdate() afterwards.
  • Comments in patterns are possible with the x modifier or (?#comment).
  • Prefer simple functions such as str_contains() and explode() for fixed text.