嵌套正则写法
$string = "some text (a(b(c)d)e) more text"; if(preg_match("/\((?>[^()]+|(?R))*\)/",$string,$matches)) { echo "<pre>"; print_r($matches); echo "</pre>"; }
<?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);