RegEx Modifiers
What Are Modifiers?
In simple terms: Modifiers are single letters placed after the closing delimiter of a pattern that change how the pattern is matched — for example, ignoring upper/lower case, or letting ^ and $ work on each line instead of the whole text.
Technically: Modifiers (also called flags) set PCRE options for the entire pattern. They are written directly after the closing delimiter, and several can be combined in any order: /pattern/imu.
preg_match('/^php$/i', 'PHP'); // i = case-insensitive
Overview
| Modifier | Name | Effect |
|---|---|---|
i | Case-insensitive | Letters match regardless of case |
m | Multiline | ^ and $ match at the start and end of each line |
s | Dotall | . also matches newline characters |
x | Extended | Whitespace in the pattern is ignored, and # starts a comment |
u | Unicode (UTF-8) | Pattern and subject are treated as UTF-8 |
D | Dollar end only | $ matches only at the very end, not before a final newline |
U | Ungreedy | Quantifiers are lazy by default (and ? makes them greedy) |
A | Anchored | The match must start at the beginning of the subject (or the given offset) |
n | No auto-capture (PHP 8.2+) | Plain (...) groups do not capture; named groups still do |
J | Duplicate names | Allows several groups with the same name |
Removed: The
emodifier (evaluate replacement as PHP code) was removed in PHP 7.0 because it caused security vulnerabilities. Usepreg_replace_callback()instead.
i — Case-Insensitive
<?php
var_dump(preg_match('/^yes$/', 'YES'));
var_dump(preg_match('/^yes$/i', 'YES'));
Output
int(0)
int(1)
Without i, yes and YES are different. With i, the comparison ignores case. For non-ASCII letters (such as É and é), combine it with u.
m — Multiline
By default, ^ matches only at the start of the whole string and $ only at its end. With m, they also match at the start and end of every line:
<?php
$list = "Shopping:\n- Milk\n- Bread\n- Eggs";
echo preg_match_all('/^- (.+)$/', $list, $withoutM) . "\n";
echo preg_match_all('/^- (.+)$/m', $list, $withM) . "\n";
print_r($withM[1]);
Output
0
3
Array
(
[0] => Milk
[1] => Bread
[2] => Eggs
)
Without m, the pattern requires the whole string to start with - , which it does not. With m, each list line is matched separately. This is essential for processing multi-line text such as logs, configuration files, or pasted lists.
s — Dot Matches Newlines
Normally, . matches any character except a newline. With s, it matches newlines too:
<?php
$html = "<p>Line one\nLine two</p>";
var_dump(preg_match('/<p>(.*)<\/p>/', $html));
preg_match('/<p>(.*)<\/p>/s', $html, $m);
var_dump($m[1]);
Output
int(0)
string(17) "Line one
Line two"
Without s, .* cannot cross the line break, so there is no match. With s, the content spanning both lines is captured.
m vs s
These two are often confused:
| Modifier | Changes |
|---|---|
m | The meaning of ^ and $ (line by line) |
s | The meaning of . (includes newlines) |
They are independent and can be combined (/.../ms).
x — Extended Mode (Readable Patterns)
With x, whitespace inside the pattern is ignored and # starts a comment. Long patterns become readable and self-documenting:
<?php
$pattern = '/
^
(?<state>[A-Z]{2}) # state code, e.g. TN
(?<district>\d{2}) # district number
(?<series>[A-Z]{1,2}) # series letters
(?<number>\d{4}) # vehicle number
$
/x';
preg_match($pattern, 'TN09AB1234', $m);
echo "{$m['state']} / {$m['district']} / {$m['series']} / {$m['number']}\n";
Output
TN / 09 / AB / 1234
To match a literal space or # in extended mode, escape it (\ ,