Heim  >  Artikel  >  Backend-Entwicklung  >  php婚配嵌套括号内的文本

php婚配嵌套括号内的文本

WBOY
WBOYOriginal
2016-06-13 13:08:21689Durchsuche

php匹配嵌套括号内的文本

本文内容, 整理自网文Finer points of PHP regular expressions. 其分析过程剥茧抽丝, 丝丝入扣, 值得一读. 该文系统地列出了PHP中正则表达式常见特性, 我只摘取其中递归部分翻译整理出来.

之前一篇文章翻译了Perl语言中的递归正则表达式. 其实不少语言中的正则都是支持递归的, 例如本文要介绍的PHP正则递归. 虽然, 工作中最常用的正则表达式都很”正则”, 只用最基本的语法就能解决85%以上的问题, 而且合理有效地使用普通正则来解决复杂问题也是一门技巧与学问; 但是高级一点的语法的确有它存的价值, 有时不用它还真办不了事儿; 况且学习正则的乐趣也在于尝试各种各样的可能性, 满足自己无穷无尽的好奇心.

本文内容, 整理自网文Finer points of PHP regular expressions. 其分析过程剥茧抽丝, 丝丝入扣, 值得一读. 该文系统地列出了PHP中正则表达式常见特性, 我只摘取其中递归部分翻译整理出来.


正文
例子
什么时候会用到递归正则表达式呢? 当然是待匹配的字串中递归地出现某种模式时(貌似废话). 最经典的例子, 就是递归正则处理嵌套括号的问题了. 例子如下.

假设你的文本中包含了正确配对的嵌套括号. 括号的深度可以是无限层. 你想捕获这样的括号组.

恕我剧透, 标准答案是这样的:

view sourceprint?
1 <?php ?
2 <span style=&quot;color: #aa7700;&quot;>$string</span> = <span style=&quot;color: #0000ff;&quot;>&quot;some text (a(b(c)d)e) more text&quot;</span>;?
3 <strong><span style=&quot;color: #006699;&quot;>if</span></strong>(preg_match(<span style=&quot;color: #0000ff;&quot;>&quot;/\(([^()]+|(?R))*\)/&quot;</span>,<span style=&quot;color: #aa7700;&quot;>$string</span>,<span style=&quot;color: #aa7700;&quot;>$matches</span>))?
4 {?
5 <span style=&quot;color: #ff1493;&quot;>echo</span> <span style=&quot;color: #0000ff;&quot;>&quot;<pre class=&quot;brush:php;toolbar:false&quot;>&quot;</pre></span>; print_r(<span style=&quot;color: #aa7700;&quot;>$matches</span>); <span style=&quot;color: #ff1493;&quot;>echo</span> <span style=&quot;color: #0000ff;&quot;>&quot;&quot;</span>;?
6 }?
7 ?>

其输出结果是:

view sourceprint?
1 Array?
2 (?
3 [0] => (a(b(c)d)e)?
4 [1] => e?
5 )

可见, 我们所需要的文本, 已经捕获到$matches[0]中了.

原理
现在思考原理.

上面的正则表达式中的关键点是(?R). (?R)的作用就是递归地替换它所在的整条正则表达式. 在每次迭代时, PHP 语法分析器都会将(?R)替换为”\(([^()]+|(?R))*\)“.

因此, 具体到上述的例子, 其正则表达式等价于:

"/\(([^()]+|\(([^()]+|\(([^()]+)*\))*\))*\)/"

但是上面的代码只适合深度为3层的括号. 对于未知深度的括号嵌套, 就只好使用这种正则了:

"/\(([^()]+|(?R))*\)/"

它不但能够匹配无限深度, 还简化了正则表达式的语法. 功能强大, 语法简洁.

现在来细看一下"/\(([^()]+|(?R))*\)/"是怎样匹配"(a(b(c)d)e)"的:

"(c)"这部分被正则式 "\(([^()]+)*\)" 匹配. 请注意, (c) 其实就相当于整个递归的一个缩影, 麻雀虽小五脏俱全, 因此它用到了整个正则表达式.
换言之, 下一步中的(c), 可以使用(?R) 来匹配.
(b(c)d)的匹配过程为:
"\("匹配"(";
"[^()]+"匹配"b";
(?R)匹配"(c)";
"[^()]+"匹配"d";
"\)"匹配")".
根据上面的匹配原理, 不难理解为什么数组的第2个元素$matches[1]与'e'等价. 子串'e'是在最后一次匹配迭代中被捕获. 匹配过程中, 只有最后一次的捕获结果才会保存到数组中.

rex注: 关于这个特性, 可以自行尝试一下, 看看使用正则式([a-z]+[0-9]+)+来匹配字串abc123xyz890, 其捕获结果$1是什么. 注意, 其结果与 Left Longest 原理并不冲突.

如果我们只需要捕获 $matches[0], 可以这样做:

view sourceprint?
1 <?php ?
2 <span style=&quot;color: #aa7700;&quot;>$string</span> = <span style=&quot;color: #0000ff;&quot;>&quot;some text (a(b(c)d)e) more text&quot;</span>;?
3 <strong><span style=&quot;color: #006699;&quot;>if</span></strong>(preg_match(<span style=&quot;color: #0000ff;&quot;>&quot;/\((?:[^()]+|(?R))*\)/&quot;</span>,<span style=&quot;color: #aa7700;&quot;>$string</span>,<span style=&quot;color: #aa7700;&quot;>$matches</span>))?
4 {?
5 <span style=&quot;color: #ff1493;&quot;>echo</span> <span style=&quot;color: #0000ff;&quot;>&quot;<pre class=&quot;brush:php;toolbar:false&quot;>&quot;</pre></span>; print_r(<span style=&quot;color: #aa7700;&quot;>$matches</span>); <span style=&quot;color: #ff1493;&quot;>echo</span> <span style=&quot;color: #0000ff;&quot;>&quot;&quot;</span>;?
6 }?
7 ?>

产生的结果相同:

view sourceprint?
1 Array?
2 (?
3 [0] => (a(b(c)d)e)?
4 )

所做的改动是捕获括号()改为非捕获捕获括号(?:)了.

还可以进一步完善为:


?

view sourceprint?
1 <?php ?
2 <span style=&quot;color: #aa7700;&quot;>$string</span> = <span style=&quot;color: #0000ff;&quot;>&quot;some text (a(b(c)d)e) more text&quot;</span>;?
3 <strong><span style=&quot;color: #006699;&quot;>if</span></strong>(preg_match("/\((?>[^()]+|(?R))*\)/",<span style=&quot;color: #aa7700;&quot;>$string</span>,<span style=&quot;color: #aa7700;&quot;>$matches</span>))?
4 {?
5 <span style=&quot;color: #ff1493;&quot;>echo</span> <span style=&quot;color: #0000ff;&quot;>&quot;<pre class=&quot;brush:php;toolbar:false&quot;>&quot;</pre></span>; print_r(<span style=&quot;color: #aa7700;&quot;>$matches</span>); <span style=&quot;color: #ff1493;&quot;>echo</span> <span style=&quot;color: #0000ff;&quot;>&quot;&quot;</span>;?
6 }?
7 ?>

这里我们用到了所谓的一次性模式(rex注: 余晟先生译的《精通正则表达式v3.0》中, 谓之”固化分组”. 可参考该书.) PHP手册也推荐只要条件允许, 就尽可能使用这种模式, 以便提升正则表达式的速度.

一次性模式很简单, 这里不再详述. 如果感兴趣, 可以参考PHP 官方手册. 如果您想深入学习PERL兼容式正则表达式, 请参考文末链接.

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel: PHP惯用的一些基本函数(一) Nächster Artikel: PHP知识点小结