PCRE pattern modifiers (i/m/s/x/u/D)
preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false
A semantics table for PCRE modifiers — i (case), m (line anchors), s (dot-all), x (free-spacing), u (UTF-8), D (strict $) — and how each language's flag system maps to them.
C C23
#include <regex.h>
#include <stdio.h>
/* Compile + execute, returning whether it hit (C's POSIX <regex.h> is the standard-library regex engine; PHP's PCRE engine IS libpcre2) */
static int try_match(const char *pattern, int cflags, const char *subject) {
regex_t re;
if (regcomp(&re, pattern, cflags) != 0) return -1;
int hit = regexec(&re, subject, 0, NULL, 0) == 0;
regfree(&re);
return hit;
}
int main(void) {
const char *s = "Hello\nWorld";
/* REG_ICASE = the i modifier */
printf("i: %d\n", try_match("hello", REG_ICASE | REG_EXTENDED, s));
printf("bare: %d\n", try_match("hello", REG_EXTENDED, s));
/* REG_NEWLINE = the m modifier (^ $ per line, . never eats a newline) */
printf("m: %d\n", try_match("^World$", REG_NEWLINE | REG_EXTENDED, s));
printf("bare m: %d\n", try_match("^World$", REG_EXTENDED, s));
printf("s: unsupported\n"); /* POSIX has no dotall */
printf("x: unsupported\n"); /* No verbose */
printf("u: unsupported\n"); /* Byte semantics, no UTF-8 switch */
printf("D: unsupported\n"); /* $ semantics decided by REG_NEWLINE */
return 0;
}
i: 1 bare: 0 m: 1 bare m: 0 s: unsupported x: unsupported u: unsupported D: unsupported
C++ C++26
#include <iostream>
#include <regex>
#include <string>
int main() {
std::string s = "Hello\nWorld";
// C++ std::regex (ECMAScript dialect): flags via constructor arguments, or inline (?i)
std::cout << "i: " << std::regex_search(s, std::regex("hello", std::regex::icase)) << "\n";
std::cout << "bare: " << std::regex_search(s, std::regex("hello")) << "\n";
std::cout << "m: " << std::regex_search(s, std::regex(R"(^World$)", std::regex::multiline)) << "\n";
std::cout << "bare m: " << std::regex_search(s, std::regex(R"(^World$)")) << "\n";
std::cout << "s: unsupported\n"; // std::regex has no dotall
std::cout << "x: unsupported\n"; // No verbose
std::cout << "u: " << std::regex_search(std::string("中"), std::regex("^.$")) << "\n"; // Byte semantics → 0
std::cout << "D: unsupported\n"; // $ does not tolerate a trailing newline by default (equivalent to PCRE's D)
return 0;
}
i: 1 bare: 0 m: 1 bare m: 0 s: unsupported x: unsupported u: 0 D: unsupported
C# v11.0.100
using System.Text.RegularExpressions;
// C# uses RegexOptions enums as flags (inline (?i) also possible); mapped against the PCRE modifiers one by one
Console.WriteLine($"i: {Regex.IsMatch("Hello\nWorld", "hello", RegexOptions.IgnoreCase)}");
Console.WriteLine($"bare: {Regex.IsMatch("Hello\nWorld", "hello")}");
Console.WriteLine($"m: {Regex.IsMatch("Hello\nWorld", "^World$", RegexOptions.Multiline)}");
Console.WriteLine($"bare m: {Regex.IsMatch("Hello\nWorld", "^World$")}");
Console.WriteLine($"s: {Regex.IsMatch("Hello\nWorld", "Hello.World", RegexOptions.Singleline)}");
Console.WriteLine($"bare s: {Regex.IsMatch("Hello\nWorld", "Hello.World")}");
Console.WriteLine($"x: {Regex.IsMatch("Hello\nWorld", "H e l l o # split apart", RegexOptions.IgnorePatternWhitespace)}");
Console.WriteLine($"u: {Regex.IsMatch("中", "^.$")}"); // Character (UTF-16) semantics
// C#'s $ tolerates a trailing newline by default (PCRE's D has no equivalent here); true end with \z
Console.WriteLine($"D: {Regex.IsMatch("Hello\nWorld\n", "world\\z")}");
i: True bare: False m: True bare m: False s: True bare s: False x: True u: True D: False
Go v1.27.4
package main
import (
"fmt"
"regexp"
)
func main() {
s := "Hello\nWorld"
// Go has no flag arguments — all modifiers go inline (?i) into the pattern (RE2 dialect)
fmt.Println("i:", regexp.MustCompile(`(?i)hello`).MatchString(s))
fmt.Println("bare:", regexp.MustCompile(`hello`).MatchString(s))
fmt.Println("m:", regexp.MustCompile(`(?m)^World$`).MatchString(s))
fmt.Println("bare m:", regexp.MustCompile(`^World$`).MatchString(s))
fmt.Println("s:", regexp.MustCompile(`(?s)Hello.World`).MatchString(s))
fmt.Println("bare s:", regexp.MustCompile(`Hello.World`).MatchString(s))
fmt.Println("x: unsupported") // RE2 has no verbose
fmt.Println("u:", regexp.MustCompile(`^.$`).MatchString("中")) // UTF-8 native
fmt.Println("D: unsupported") // $ does not tolerate a trailing newline by default (equivalent to PCRE's D)
}
i: true bare: false m: true bare m: false s: true bare s: false x: unsupported u: true D: unsupported
Java v27.0.1
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String s = "Hello\nWorld";
// Java uses Pattern compile constants as flags (modifiers can also go inline (?i), but constants are idiomatic)
System.out.println("i: " + Pattern.compile("hello", Pattern.CASE_INSENSITIVE).matcher(s).find());
System.out.println("bare: " + Pattern.compile("hello").matcher(s).find());
System.out.println("m: " + Pattern.compile("^World$", Pattern.MULTILINE).matcher(s).find());
System.out.println("bare m: " + Pattern.compile("^World$").matcher(s).find());
System.out.println("s: " + Pattern.compile("Hello.World", Pattern.DOTALL).matcher(s).find());
System.out.println("bare s: " + Pattern.compile("Hello.World").matcher(s).find());
System.out.println("x: " + Pattern.compile("H e l l o # split apart", Pattern.COMMENTS).matcher(s).find());
System.out.println("u: " + Pattern.compile("^.$").matcher("中").find()); // Character (UTF-16) semantics
// Java's $ tolerates a trailing newline by default (PCRE's D has no equivalent here); true end with \z
System.out.println("D: " + Pattern.compile("world\\z").matcher("Hello\nWorld\n").find());
}
}
i: true bare: false m: true bare m: false s: true bare s: false x: true u: true D: false
JavaScript v26.13.1
const s = "Hello\nWorld";
// i: case-insensitive
console.log("i:", /hello/i.test(s));
console.log("bare:", /hello/.test(s));
// m: ^ $ per line
console.log("m:", /^World$/m.test(s));
console.log("bare m:", /^World$/.test(s));
// s: dotAll (ES2018) lets . match newlines
console.log("s:", /Hello.World/s.test(s));
console.log("bare s:", /Hello.World/.test(s));
console.log("x: unsupported"); // JS has no verbose mode
// JS's /u is the unicode mode (code point/surrogate pair semantics), not PCRE's UTF-8 switch;
// . works on UTF-16 code units by default — one CJK char is 1 code unit, no flag needed
console.log("u:", /^.$/.test("中"));
// JS's $ does not tolerate a trailing newline by default (naturally equivalent to PCRE's D)
console.log("D:", /world$/.test("Hello\nWorld\n"));
i: true bare: false m: true bare m: false s: true bare s: false x: unsupported u: true D: false
PHP v8.6.0
$subject = "Hello\nWorld";
// i: case-insensitive (case-sensitive by default)
var_dump((bool)preg_match('/hello/i', $subject));
var_dump((bool)preg_match('/hello/', $subject));
// m: ^ $ match per line (by default ^ only at string start, $ at string end or before a trailing newline)
var_dump((bool)preg_match('/^World$/m', $subject));
var_dump((bool)preg_match('/^World$/', $subject));
// s: . matches newlines (by default . does not)
var_dump((bool)preg_match('/Hello.World/s', $subject));
var_dump((bool)preg_match('/Hello.World/', $subject));
// x: in-pattern whitespace and # comments are ignored
var_dump((bool)preg_match('/H e l l o # split apart/x', $subject));
// u: UTF-8 mode — . matches per character, not per byte (one CJK char is 3 bytes, 1 character)
var_dump((bool)preg_match('/^.$/u', "中"));
var_dump((bool)preg_match('/^.$/', "中"));
// D: $ no longer tolerates a trailing newline (by default $ matches World before the trailing newline of "World\n")
var_dump((bool)preg_match('/World$/D', "Hello\nWorld\n"));
var_dump((bool)preg_match('/World$/', "Hello\nWorld\n"));
bool(true) bool(false) bool(true) bool(false) bool(true) bool(false) bool(true) bool(true) bool(false) bool(false) bool(true)
Python v3.15.2
import re
s = "Hello\nWorld"
# i: re.I case-insensitive
print("i:", bool(re.search(r"hello", s, re.I)))
print("bare:", bool(re.search(r"hello", s)))
# m: re.M makes ^ $ match per line
print("m:", bool(re.search(r"^World$", s, re.M)))
print("bare m:", bool(re.search(r"^World$", s)))
# s: re.S lets . match newlines
print("s:", bool(re.search(r"Hello.World", s, re.S)))
print("bare s:", bool(re.search(r"Hello.World", s)))
# x: re.X ignores in-pattern whitespace and # comments
print("x:", bool(re.search(r"H e l l o # split apart", s, re.X)))
# u: str patterns are Unicode-aware by nature — py2's re.U is default behavior in py3
print("u:", bool(re.search(r"^.$", "中")))
# D: no equivalent flag — $ tolerates a trailing newline by default; true end with \Z
print("D:", bool(re.search(r"world\Z", "Hello\nWorld\n")))
i: True bare: False m: True bare m: False s: True bare s: False x: True u: True D: False
Rust v1.99.1
use regex::Regex;
fn main() {
let s = "Hello\nWorld";
// The regex crate mirrors Go: modifiers go inline (?i) into the pattern
println!("i: {}", Regex::new(r"(?i)hello").unwrap().is_match(s));
println!("bare: {}", Regex::new(r"hello").unwrap().is_match(s));
println!("m: {}", Regex::new(r"(?m)^World$").unwrap().is_match(s));
println!("bare m: {}", Regex::new(r"^World$").unwrap().is_match(s));
println!("s: {}", Regex::new(r"(?s)Hello.World").unwrap().is_match(s));
println!("bare s: {}", Regex::new(r"Hello.World").unwrap().is_match(s));
println!("x: {}", Regex::new(r"(?x) H e l l o").unwrap().is_match(s)); // (?x) verbose
println!("u: {}", Regex::new(r"^.$").unwrap().is_match("中")); // Unicode native
println!("D: unsupported"); // $ does not tolerate a trailing newline by default (equivalent to PCRE's D)
}
i: true bare: false m: true bare m: false s: true bare s: false x: true u: true D: unsupported
TypeScript v26.13.1
const s = "Hello\nWorld";
// i: case-insensitive
console.log("i:", /hello/i.test(s));
console.log("bare:", /hello/.test(s));
// m: ^ $ per line
console.log("m:", /^World$/m.test(s));
console.log("bare m:", /^World$/.test(s));
// s: dotAll (ES2018) lets . match newlines
console.log("s:", /Hello.World/s.test(s));
console.log("bare s:", /Hello.World/.test(s));
console.log("x: unsupported"); // JS has no verbose mode
// JS's /u is the unicode mode (code point/surrogate pair semantics), not PCRE's UTF-8 switch;
// . works on UTF-16 code units by default — one CJK char is 1 code unit, no flag needed
console.log("u:", /^.$/.test("中"));
// JS's $ does not tolerate a trailing newline by default (naturally equivalent to PCRE's D)
console.log("D:", /world$/.test("Hello\nWorld\n"));
i: true bare: false m: true bare m: false s: true bare s: false x: unsupported u: true D: false
Language differences
PHP is PCRE's native habitat (the engine is libpcre2 itself) — modifiers trail the delimiters, as in /pattern/i; the cross-language flag mapping: i=case-insensitive, m=line anchors, s=dot-all, x=free-spacing, u=UTF-8, D=strict $.
C: the standard-library regex is POSIX <regex.h> — REG_ICASE≈i, REG_NEWLINE≈m; no dotall/verbose/UTF-8 switches; PHP's PCRE engine is itself libpcre2 (a C third-party library).
C++: std::regex (ECMAScript dialect) — icase≈i, multiline≈m (C++17); no dotall/verbose; byte semantics ("中" is 3 bytes, ^.$ misses).
C#: RegexOptions flags — IgnoreCase/Multiline/Singleline/IgnorePatternWhitespace map to i/m/s/x; $ tolerates a trailing newline by default (PCRE's no-D behavior), true end is \z; "中" is one UTF-16 char.
Go: the RE2 dialect — no flag parameter, modifiers inline as (?i)/(?m)/(?s); x unsupported; $ never tolerates a trailing newline (naturally D-like); UTF-8 native so no u — its "x: unsupported" output is the biggest outlier of the ten.
Java: Pattern compile constants — CASE_INSENSITIVE/MULTILINE/DOTALL/COMMENTS map to i/m/s/x; $ tolerates a trailing newline, true end \z; "中" is one UTF-16 char.
JavaScript: regex literal flags — i/m/s (dotAll, ES2018) align; no x; /u is the unicode mode (code-point/surrogate semantics), not PCRE's UTF-8 switch; . matches one UTF-16 code unit by default ("中" hits immediately); $ never tolerates a trailing newline (naturally D-like).
PHP: the full modifier family — i/m/s/x/u/D plus A (anchored), S (pre-analysis cache), U (inverted greed); u is the only text-encoding switch: without it matching is byte-based ("中" is 3 bytes and ^.$ misses); invalid UTF-8 input makes the whole preg call return false.
Python: the re module flags — re.I/re.M/re.S/re.X align; str patterns are natively Unicode (py2's re.U is the py3 default); $ tolerates a trailing newline, true end is \Z.
Rust: the regex crate is Go-like — modifiers inline as (?i)/(?m)/(?s)/(?x); Unicode native; $ never tolerates a trailing newline.
TypeScript: same as JavaScript (identical flag spelling; nothing extra at the type level).