PHP Regex Cheat Sheet
Basic Patterns
| Pattern | Description |
|---|---|
| . | Matches any single character except a newline. |
| ^ | Matches the start of a string. |
| $ | Matches the end of a string. |
| * | Matches 0 or more repetitions of the preceding character or group. |
| + | Matches 1 or more repetitions of the preceding character or group. |
| ? | Matches 0 or 1 repetition of the preceding character or group. |
| {n} | Matches exactly n repetitions of the preceding character or group. |
| {n,} | Matches n or more repetitions of the preceding character or group. |
| {n,m} | Matches between n and m repetitions of the preceding character or group. |
Character Classes
| Pattern | Description |
|---|---|
| \d | Matches any digit (0-9). |
| \D | Matches any non-digit. |
| \w | Matches any word character (alphanumeric + underscore). |
| \W | Matches any non-word character. |
| \s | Matches any whitespace character (spaces, tabs, line breaks). |
| \S | Matches any non-whitespace character. |
Assertions
| Pattern | Description |
|---|---|
| \b | Matches a word boundary. |
| \B | Matches a non-word boundary. |
| (?=...) | Positive lookahead. |
| (?!...) | Negative lookahead. |
Groups and Backreferences
| Pattern | Description |
|---|---|
| ( ... ) | Captures the matched content. |
| (?: ... ) | Non-capturing group. |
| \1 | Backreference to the first capturing group. |
| \2 | Backreference to the second capturing group, and so on. |
Modifiers
| Modifier | Description |
|---|---|
| i | Case-insensitive search. |
| m | Multiline search. |
| s | Dot matches newline. |
| x | Extended. Whitespace data characters in the pattern are ignored. |
| U | Ungreedy pattern. Will take the smallest match. |
Escape Sequences
| Pattern | Description |
|---|---|
| \A | Matches the start of the subject string. |
| \Z | Matches the end of the subject or the newline at the end. |
| \z | Matches the end of the subject. |
| \G | Matches the point where the last match finished. |
| \Q...\E | Quotes any metacharacters in the ... data between. |
Character Types
| Pattern | Description |
|---|---|
| \C | Matches a single byte in UTF-8 mode. |
| \R | Matches any newline sequence. |
| \X | Matches an extended Unicode sequence. |
Unicode Property Escapes
| Pattern | Description |
|---|---|
| \p{...} | Matches a character with a particular Unicode property. |
| \P{...} | Matches a character without a particular Unicode property. |
Atomic Grouping and Possessive Quantifiers
| Pattern | Description |
|---|---|
| (?>...) | Atomic group. |
| *+ | Possessive quantifier (0 or more). |
| ++ | Possessive quantifier (1 or more). |
| ?+ | Possessive quantifier (0 or 1). |
Lookbehind Assertions
| Pattern | Description |
|---|---|
| (?<=...) | Positive lookbehind. |
| (? | Negative lookbehind. |
Conditional Patterns
| Pattern | Description |
|---|---|
| (?(condition)yes-pattern|no-pattern) | Conditional pattern. |
Comments
| Pattern | Description |
|---|---|
| (?#...) | Comment; the ... content is ignored. |
Modifiers
| Modifier | Description |
|---|---|
| J | Allows duplicate named capture groups. |
| N | Disables capturing groups (named and unnamed). |
| U | Makes the quantifiers lazy by default. |
Recursion and Subroutine Calls
| Pattern | Description |
|---|---|
| (?R) | Recurses the entire pattern. |
| (?1) | Recurses the first capturing group. |
| (?&name) | Recurses the named capturing group. |
| (?P>name) | Another way to recurse the named capturing group. |
Callouts
| Pattern | Description |
|---|---|
| (?C) | Callout, typically used for debugging. |
Relative Backreferences
| Pattern | Description |
|---|---|
| \g{-1} | Backreference to the most recently opened capturing group. |
Named Patterns
| Pattern | Description |
|---|---|
| (?P |
Define a named capturing group. |
| (?&name) | Subroutine call to a named group. |
| (?P=name) | Backreference to a named group. |
Control Verbs
| Pattern | Description |
|---|---|
| (*ACCEPT) | Ends the match successfully, as if the end of the subject string is reached. |
| (*FAIL) | Forces the pattern to fail. |
| (*MARK:NAME) | Sets a mark that can be returned on failure. |
Branch Reset
| Pattern | Description |
|---|---|
| (?|...) | Branch reset group. Alternatives inside share the same capturing groups. |