[^()]+|(?R))*)/",$string,$matches)) { echo ""; p"/> [^()]+|(?R))*)/",$string,$matches)) { echo ""; p">
Home > Backend Development > PHP Tutorial > 嵌套正则写法,该怎么解决

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

WBOY
Release: 2016-06-13 13:05:34
Original
914 people have browsed it

嵌套正则写法

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$string = "some text (a(b(c)d)e) more text"; 
if(preg_match("/\((?>[^()]+|(?R))*\)/",$string,$matches)) 
{ 
echo "<pre class="brush:php;toolbar:false">"; print_r($matches); echo "
Copy after login
"; }

上面是网上找到的嵌套正则写法,把string改成:
$string = 'some text
cd
e
more text';

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

最好能做到个数不确定的嵌套匹配

------解决方案--------------------
PHP code
<?php $string = 'some text <tag:cate name="a">a<cate name="b">b<cate>c</cate>d</cate>e more text';
preg_match("/<cate name='\"\w+\")?'>(?>\w+|(?R))*/",$string,$matches) &&
    print_r($matches); <div class="clear">
                 
              
              
        
            </div></cate>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template