这些函数中使用的模式语法非常类似 perl。表达式必须用分隔符闭合,比如一个正斜杠(/)。 分隔符可以使任意非字母数字,除反斜杠(\)和空字节之外的非空白 ascii 字符。 如果分隔符 在表达式中使用,需要使用反斜线进行转义。自php 4.0.4开始,可以使用 perl 样式的()、 {}、 [] 以及 <> 作为分隔符。 更详细的解释参见模式语法。
结束分隔符后面可以紧跟模式修饰符来影响匹配效果。 参见模式修饰符。
PHP也支持使用 POSIX 扩展正则表达式函数 的 POSIX 扩展语法的正则表达式。
Note:
这个扩展维护了一个已编译正则表达式的全局线程化缓存(最大4096)。
你应该知道一些 PCRE 的限制。阅读» http://www.pcre.org/pcre.txt 获取更详细信息。
PCRE 库是一个实现了与 perl 5 在语法和语义上略有差异(详见下文)的正则表达式模式匹配功能的函数集。 当前的实现对应于 perl 5.005。
m dot colombo at NOSPAM dot esi dot it (2009-10-12 03:22:58)
Converting POSIX regex patters to PCRE is non-trivial. I had many (about 17.000) patterns stored in a DB, and found that surrounding one with delimiters (such as '/'), after quoting them, is not enough.
I've been biten by a \r that slipped into a pattern (that was supposed to be \.r and became .\r instead). Too bad in PCRE it stands for carriage-return.
AFAIK, there's no way to disable this behaviour and make PCRE more POSIX compatibile. You have to look for every single escape sequence that has no meaning in POSIX regex but has one in PCRE and remove the backslash.
Before POSIX regex are removed it would be really cool to have a function that converts a POSIX regex pattern into the equivalent PCRE one.