在结束分隔符之后可以跟随多种模式修饰符 (用php 的术语来说,叫做 pattern modifier),
在某些情况下,修饰符也可以出现在正则表达式内部,修饰模式的某些性质。我们已经在一些例子中看到过表达不区分大小写的模式修饰符 i 。下面简要介绍模式修饰符:
修饰符 |
表达式中的写法 |
说明 |
i |
(?i) |
Ø 忽略大小写 |
m |
(?m) |
Ø 增强行锚点模式 |
s |
(?s) |
Ø 点号通配模式 |
x |
(?x) |
Ø 宽松排列和注释模式 |
u |
|
Ø 以UTF-8读取正则表达式和目标字符串 |
X |
(?X) |
Ø 启用PCRE “额外功能(extra stuff)” |
e |
|
Ø 将replacement作为PHP代码(只用于preg_replace) |
S |
|
Ø 启用PCRE的“study”优化尝试 |
下面三个很少用到 |
||
U |
(?U) |
Ø 交换 "*"和"*?"的匹配优先含义 |
A |
|
Ø 将整个匹配尝试锚定在起始位置 |
D |
|
Ø "$1" 只能匹配EOS,而不是EOS之前的换行符 (如果使用了模式修饰符m则不会这样) |
表达式内部的模式修饰符
在正则表达式内部,模式修饰符可以单独出现,来启用或停用某些特性(例如用"(?i)"来启用不区分大小写匹配,用"(?-i)"来停用。此时,它们的作用范围持续到对应的结束括号,如果不存在,就持续到正则表达式的末尾。
它们也可以用作模式修饰范围 例如 "(?i...)" 表示对此括号内的内容进行不区分大小写匹配"(?-sm:...)" 表示在此范围内停用s和m模式.
正则表达式之外,结束分隔符之后的模式修饰符可以以任何顺序组织,下例中的‘si’表示同时启用不区分大小写和点号通配模式:
If(preg_match('{
摘自lostwolf's Blog

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor