preg_match("/([[:alpha:]]+)\s+\1/", "Paris in the the spring", $m);
//
返回
true
且
$m[1]
是
"the"
preg_match()
最多可以捕获
99
个子模式,大于第
99
个的子模式将被忽略。
后缀选项
Perl
风格的正则表达式允许把一个单字符(标志)放在正则表达式的后面,从而改变表
达式的解析、行为或者匹配方式。例如,要匹配大小写敏感,只需要在后面加一个
i
标志:
preg_match("/cat/i", "Stop, Catherine!"); //
返回
true
表
4-10
展示了
Perl
兼容的正则表达式支持的
Perl
里面的修饰符。
表4-10:Perl标志
修饰符含义
/
regexp
/i
匹配大小写
/
regexp
/s
使句点匹配任何字符,包括换行符
\n
/
regexp
/x
去掉空白和注释
/
regexp
/m
使得
^
匹配换行符
\n
之后的内容,
$
匹配换行符
\n
之前的内容
/
regexp
/e
如果替换字符串是
PHP
代码,使用
eval()
执行该代码,可得到实际替
换的字符串
PHP
中的
Perl
兼容的正则表达式函数也支持在
Perl
中不支持的其他修饰符,如表
4-11
所示。
表4-11:其他PHP修饰符
修饰符含义
/
regexp
/U
颠倒子模式的贪婪性。
*
和
+
尽可能少地匹配而不是尽可能多
/
regexp
/u
把模式字符串当作
UTF-8
编码
/
regexp
/X
如果一个反斜杠之后跟着没有特殊意义的字符,则产生一个错误 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.