PCRE 修饰符(i/m/s/x/u/D)
preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false
PCRE 修饰符语义对照表:i(大小写)/m(逐行锚)/s(点号吞换行)/x(宽松模式)/u(UTF-8)/D($ 严格结尾),及各语言 flag 体系的映射差异。
C C23
#include <regex.h>
#include <stdio.h>
/* 编译 + 执行,返回是否命中(C 的 POSIX <regex.h> 是标准库正则;PHP 的 PCRE 引擎即 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 = i 修饰符(大小写不敏感) */
printf("i: %d\n", try_match("hello", REG_ICASE | REG_EXTENDED, s));
printf("bare: %d\n", try_match("hello", REG_EXTENDED, s));
/* REG_NEWLINE = m 修饰符(^ $ 逐行、. 不吃换行) */
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 无 dotall */
printf("x: unsupported\n"); /* 无 verbose */
printf("u: unsupported\n"); /* 字节语义,无 UTF-8 开关 */
printf("D: unsupported\n"); /* $ 语义由 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 方言):构造参数给 flag,也可内联 (?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";
// multiline(C++17):^ $ 逐行
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 无 dotall
std::cout << "x: unsupported\n"; // 无 verbose
// 字节语义:"中" 是 3 字节,^.$ 匹配单字节 → 0
std::cout << "u: " << std::regex_search(std::string("中"), std::regex("^.$")) << "\n";
std::cout << "D: unsupported\n"; // $ 默认不容忍结尾换行(等价 PCRE 的 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# 用 RegexOptions 枚举做 flag(也可内联 (?i));逐一对照 PCRE 修饰符
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 # 拆开写", RegexOptions.IgnorePatternWhitespace)}");
// 字符(UTF-16)语义:"中" 是 1 个 char,无需 UTF-8 开关
Console.WriteLine($"u: {Regex.IsMatch("中", "^.$")}");
// C# $ 默认容忍结尾换行(等价 PCRE 无 D);真结尾用 \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 无 flag 参数——修饰符全部内联 (?i) 写进模式(RE2 方言)
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 无 verbose
fmt.Println("u:", regexp.MustCompile(`^.$`).MatchString("中")) // UTF-8 原生,无需开关
fmt.Println("D: unsupported") // $ 默认不容忍尾部换行(等价 PCRE 的 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 用 Pattern 编译常量做 flag(修饰符同样可以内联 (?i),但惯用常量)
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 # 拆开写", Pattern.COMMENTS).matcher(s).find());
// 字符(UTF-16)语义:"中" 是 1 个 char,无需 UTF-8 开关
System.out.println("u: " + Pattern.compile("^.$").matcher("中").find());
// Java $ 默认容忍结尾换行(等价 PCRE 无 D);真结尾用 \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:大小写不敏感
console.log("i:", /hello/i.test(s));
console.log("bare:", /hello/.test(s));
// m:^ $ 逐行
console.log("m:", /^World$/m.test(s));
console.log("bare m:", /^World$/.test(s));
// s:dotAll(ES2018)让 . 匹配换行
console.log("s:", /Hello.World/s.test(s));
console.log("bare s:", /Hello.World/.test(s));
console.log("x: unsupported"); // JS 无 verbose 模式
// JS 的 /u 是 unicode 模式(码位/代理对语义),不是 PCRE 的 UTF-8 开关;
// . 默认按 UTF-16 码元——"中" 是 1 码元,无需任何 flag
console.log("u:", /^.$/.test("中"));
// JS 的 $ 默认不容忍结尾换行(天然等价 PCRE 的 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:大小写不敏感(默认敏感)
var_dump((bool)preg_match('/hello/i', $subject));
var_dump((bool)preg_match('/hello/', $subject));
// m:^ $ 逐行匹配(默认 ^ 只在串首、$ 在串尾或尾部换行前)
var_dump((bool)preg_match('/^World$/m', $subject));
var_dump((bool)preg_match('/^World$/', $subject));
// s:. 匹配换行(默认 . 不匹配换行)
var_dump((bool)preg_match('/Hello.World/s', $subject));
var_dump((bool)preg_match('/Hello.World/', $subject));
// x:模式内空白与 # 注释被忽略
var_dump((bool)preg_match('/H e l l o # 拆开写/x', $subject));
// u:UTF-8 模式——. 按字符而非字节("中" 是 3 字节 1 字符)
var_dump((bool)preg_match('/^.$/u', "中"));
var_dump((bool)preg_match('/^.$/', "中"));
// D:$ 不再容忍结尾换行(默认 $ 匹配 "World\n" 尾换行前的 World)
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 大小写不敏感
print("i:", bool(re.search(r"hello", s, re.I)))
print("bare:", bool(re.search(r"hello", s)))
# m:re.M 让 ^ $ 逐行匹配
print("m:", bool(re.search(r"^World$", s, re.M)))
print("bare m:", bool(re.search(r"^World$", s)))
# s:re.S 让 . 匹配换行
print("s:", bool(re.search(r"Hello.World", s, re.S)))
print("bare s:", bool(re.search(r"Hello.World", s)))
# x:re.X 忽略模式内空白与 # 注释
print("x:", bool(re.search(r"H e l l o # 拆开写", s, re.X)))
# u:str 模式天然按 Unicode 字符——py2 的 re.U 在 py3 是默认行为
print("u:", bool(re.search(r"^.$", "中")))
# D:无等价 flag——$ 默认容忍尾部换行,真结尾用 \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";
// regex crate 同 Go:修饰符内联 (?i) 写进模式
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) 宽松模式
println!("u: {}", Regex::new(r"^.$").unwrap().is_match("中")); // Unicode 原生,无需开关
println!("D: unsupported"); // $ 默认不容忍尾部换行(等价 PCRE 的 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:大小写不敏感
console.log("i:", /hello/i.test(s));
console.log("bare:", /hello/.test(s));
// m:^ $ 逐行
console.log("m:", /^World$/m.test(s));
console.log("bare m:", /^World$/.test(s));
// s:dotAll(ES2018)让 . 匹配换行
console.log("s:", /Hello.World/s.test(s));
console.log("bare s:", /Hello.World/.test(s));
console.log("x: unsupported"); // JS 无 verbose 模式
// JS 的 /u 是 unicode 模式(码位/代理对语义),不是 PCRE 的 UTF-8 开关;
// . 默认按 UTF-16 码元——"中" 是 1 码元,无需任何 flag
console.log("u:", /^.$/.test("中"));
// JS 的 $ 默认不容忍结尾换行(天然等价 PCRE 的 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 是 PCRE 修饰符的母语环境(引擎即 libpcre2)——修饰符串写在中定界符后,如 /pattern/i;各家 flag 体系与 PCRE 修饰符逐一对照:i=大小写不敏感、m=^ $ 逐行、s=点号吞换行、x=宽松模式、u=UTF-8、D=$ 严格结尾。
C:标准库正则是 POSIX <regex.h>——REG_ICASE≈i、REG_NEWLINE≈m;无 dotall/verbose/UTF-8 开关;PHP 的 PCRE 引擎本身就是 libpcre2(C 的第三方库)。
C++:std::regex(ECMAScript 方言)——icase≈i、multiline≈m(C++17);无 dotall/verbose;字节语义("中" 3 字节,^.$ 不命中)。
C#: RegexOptions 枚举——IgnoreCase/Multiline/Singleline/IgnorePatternWhitespace 逐一对应 i/m/s/x;$ 默认容忍结尾换行(PCRE 无 D 行为),真结尾用 \z;"中" 按 UTF-16 是 1 个 char。
Go:RE2 方言——没有 flag 参数,修饰符全部内联 (?i)/(?m)/(?s);不支持 x;$ 默认不容忍尾部换行(天然等价 D);UTF-8 原生无需 u——示例输出 "x: unsupported" 是十门语言里差异最大的。
Java:Pattern 编译常量——CASE_INSENSITIVE/MULTILINE/DOTALL/COMMENTS 对应 i/m/s/x;$ 默认容忍结尾换行,真结尾 \z;"中" 按 UTF-16 是 1 个 char。
JavaScript:正则字面量 flag——i/m/s(dotAll,ES2018)对齐;无 x;/u 是 unicode 模式(码位/代理对语义),不是 PCRE 的 UTF-8 开关;. 默认按 UTF-16 码元("中" 1 码元直接命中);$ 默认不容忍结尾换行(天然等价 D)。
PHP:修饰符全家桶——i/m/s/x/u/D,还有 A(锚定开头)/S(预分析缓存)/U(反转贪婪)等;u 是唯一「文本编码」开关:按字节匹配(无 u 时 "中" 3 字节 ^.$ 不命中);非法 UTF-8 输入时整个 preg 返回 false。
Python:re 模块 flag——re.I/re.M/re.S/re.X 对齐;str 模式天然 Unicode(py2 的 re.U 在 py3 是默认);$ 默认容忍结尾换行,真结尾用 \Z。
Rust:regex crate 同 Go——修饰符内联 (?i)/(?m)/(?s)/(?x);Unicode 原生;$ 默认不容忍尾部换行。
TypeScript:同 JavaScript(flag 写法一致;类型层面无额外修饰符)。