Skip to content
Tutorials Center
My learning

Topics

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

PHP i, m, s and D on a three-line 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.

PHP x, u, U, A and n

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

  • u is the one you will use most. Add it whenever text may contain non-ASCII characters. It also validates the subject: bad UTF-8 returns false instead of a wrong answer (see preg_last_error()). Offsets reported by PREG_OFFSET_CAPTURE are still byte offsets.
  • m changes only ^ and $. It does not make . match newlines; that is s.
  • $ and trailing newlines. Without D, /^\d+$/ accepts "42\n". Use D or \z for strict validation, especially for form input.
  • i and Unicode. Case-insensitive matching of non-ASCII letters works only with u.
  • x and spaces. In extended mode a literal space needs \ or [ ], and a # inside the pattern needs a backslash. Do not put unescaped user text into an x pattern; see preg_quote().
  • U is 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 false result, as does the removed e modifier.
  • n needs PHP 8.2 or later. Numbered references such as $1 then refer to nothing unless the group is named.