教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP中的递归正则表达式用法

PHP中的递归正则表达式用法

发布时间:2017-10-25   编辑:jiaochengji.com
教程集为您提供PHP中的递归正则表达式用法等资源,欢迎您收藏本站,我们将为您提供最新的PHP中的递归正则表达式用法资源

例子如下.

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

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1924')"><textarea id="copy1924" rows="10" cols="40" style="display: none;"></textarea></td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1924>

<?php
$string = "some text (a(b(c)d)e) more text";
if(preg_match("/(([^()] |(?R))*)/",$string,$matches))
{
    echo "<pre>"; print_r($matches); echo "</pre>";
}
?>
其输出结果是:


 Array
(
    [0] => (a(b(c)d)e)
    [1] => e   
)

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

原理
现在思考原理.

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

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2750')"><textarea id="copy2750" rows="10" cols="40" style="display: none;"></textarea></td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2750>

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

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4303')"><textarea id="copy4303" rows="10" cols="40" style="display: none;"></textarea></td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4303>

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

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

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

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

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

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5846')"><textarea id="copy5846" rows="10" cols="40" style="display: none;"></textarea></td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5846>

 <?php
    $string = "some text (a(b(c)d)e) more text";
    if(preg_match("/((?:[^()] |(?R))*)/",$string,$matches))
    {
        echo "<pre>"; print_r($matches); echo "</pre>";
    }
?>
产生的结果相同:


 Array
    (
     [0] => (a(b(c)d)e)
    )

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

还可以进一步完善为:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7129')"><textarea id="copy7129" rows="10" cols="40" style="display: none;"></textarea></td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7129> <?php
    $string = "some text (a(b(c)d)e) more text";
    if(preg_match("/((?>[^()] |(?R))*)/",$string,$matches))
    {
        echo "<pre>"; print_r($matches); echo "</pre>";
    }
?>

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

您可能感兴趣的文章:
一文了解Python中的递归
php正则ereg ereg_replace eregi eregi_replace split
PHP正则过滤文章中图片的方法
正则表达式 模式匹配 Javascript
正则表达式使用详解
php正则表达式匹配中文的二个例子
php邮箱检测的正则表达式一例
php正则表达式转义字符的例子
正则表达式在网络编程中的运用
php正则表达式是什么?(代码实例)

[关闭]
~ ~