PHP Reference · Regular Expressions
PHP regex pattern modifiers
Reference for PHP PCRE pattern modifiers: i, m, s, x, u, U, D, A, S, X, n and J. What each flag changes, with examples, inline options and common mistakes.
Modifiers are the letters after the closing delimiter of a pattern, as in '/abc/iu'. Each one switches a behavior of the whole expression. You can combine any number of them in any order, and you can also turn most of them on or off inside the pattern with inline options such as (?i) and (?-i).
<?php var_dump(preg_match('/hello/i', 'HELLO world'));
Modifier table
| Modifier | Name | Effect |
|---|---|---|
i |
Case-insensitive | Letters match in either case. |
m |
Multiline | ^ and $ match at the start and end of every line. |
s |
Dotall | . also matches newline characters. |
x |
Extended | Whitespace in the pattern is ignored and # starts a comment; escape or put a space in a class to match one. |
u |
UTF-8 | The pattern and subject are UTF-8: . is one character, \p{…} works, and invalid UTF-8 makes the call fail. |
U |
Ungreedy | Swaps greedy and lazy: * becomes lazy, *? becomes greedy. |
D |
Dollar end only | $ matches only at the very end, not before a final newline. |
A |
Anchored | The match must start at the beginning (or at the offset). |
S |
Study | Obsolete; accepted but has no useful effect. |
X |
Extra | Rarely needed; leave it out. |
n |
No auto-capture | (PHP 8.2+) Plain (…) groups do not capture; only named groups do. |
J |
Duplicate names | Allows the same group name twice. |
Try it
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.
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
uis the one you will use most. Add it whenever text may contain non-ASCII characters. It also validates the subject: bad UTF-8 returnsfalseinstead of a wrong answer (seepreg_last_error()). Offsets reported byPREG_OFFSET_CAPTUREare still byte offsets.mchanges only^and$. It does not make.match newlines; that iss.$and trailing newlines. WithoutD,/^\d+$/accepts"42\n". UseDor\zfor strict validation, especially for form input.iand Unicode. Case-insensitive matching of non-ASCII letters works only withu.xand spaces. In extended mode a literal space needs\or[ ], and a#inside the pattern needs a backslash. Do not put unescaped user text into anxpattern; seepreg_quote().Uis rarely a good idea. It reverses the meaning of?after quantifiers, which surprises readers. Prefer explicit lazy quantifiers like.*?.- Inline options apply to the end of the current group:
(?i)at the top switches case-insensitivity on for the rest of the pattern,(?-i)turns it off, and(?i:…)limits it to the inner group. - Unknown modifiers produce a warning and a
falseresult, as does the removedemodifier. nneeds PHP 8.2 or later. Numbered references such as$1then refer to nothing unless the group is named.