首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

嵌套正则写法,该怎么解决

2012-11-20 
嵌套正则写法PHP code$string some text (a(b(c)d)e) more text if(preg_match(/\((?[^()]+|(?R))*

嵌套正则写法

PHP code
$string = "some text (a(b(c)d)e) more text"; if(preg_match("/\((?>[^()]+|(?R))*\)/",$string,$matches)) { echo "<pre>"; print_r($matches); echo "</pre>"; } 

上面是网上找到的嵌套正则写法,把string改成:
$string = 'some text <tag:cate name="a"><div></div><tag:cate name="b"><div></div><tag:cate>c</tag:cate>d</tag:cate>e</tag:cate> more text';

改成这样后,正则规则我换不过来了,亲大虾帮忙改一下。

最好能做到<tag:cate name="n"></tag:cate>个数不确定的嵌套匹配

[解决办法]
PHP code
<?php$string = 'some text <tag:cate name="a">a<tag:cate name="b">b<tag:cate>c</tag:cate>d</tag:cate>e</tag:cate> more text';preg_match("/<tag:cate(?: name=\"\w+\")?>(?>\w+|(?R))*<\/tag:cate>/",$string,$matches) &&    print_r($matches); 

热点排行