简体   繁体   English

无论如何要清理正则表达式捕获组?

[英]Anyway to clean the regex capture groups up?

I am trying to clean up this regex capture groups because it's capturing correctly just to me it's a strange return for preg_match_all strange to me since it's first time using preg_match_all 我正在尝试清理此正则表达式捕获组,因为它对我来说正确捕获,这对preg_match_all来说是一个奇怪的回报,对我来说这很奇怪,因为这是第一次使用preg_match_all

Test Code:: http://ideone.com/7NnrI1 测试代码:: http://ideone.com/7NnrI1

Regex 正则表达式

/(?=<!--\sBEGIN\s(.*?)\s-->(.*?)(?=<!--\sEND\s(.*?)\s-->))/s

Return 返回

Array
(
[0] => Array
    (
        [0] => Array
            (
                [0] => 
                [1] => 0
            )

        [1] => Array
            (
                [0] => 
                [1] => 326
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [0] => switch_legend
                [1] => 11
            )

        [1] => Array
            (
                [0] => switch_default
                [1] => 337
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [0] => 
<ul id="picture_legend">
    <li><img src="{FORUM_NEW_IMG}" alt="{L_NEW_POSTS}" />{L_NEW_POSTS}</li>
    <li><img src="{FORUM_IMG}" alt="{L_NO_NEW_POSTS}" />{L_NO_NEW_POSTS}</li>
     <li><img src="{FORUM_LOCKED_IMG}" alt="{L_LOCKED}" />{L_LOCKED}</li>
</ul>

                [1] => 28
            )

        [1] => Array
            (
                [0] => 
<ul id="picture_legend">
    <li><img src="{FORUM_NEW_IMG}" alt="{L_NEW_POSTS}" />{L_NEW_POSTS}</li>
    <li><img src="{FORUM_IMG}" alt="{L_NO_NEW_POSTS}" />{L_NO_NEW_POSTS}</li>
     <li><img src="{FORUM_LOCKED_IMG}" alt="{L_LOCKED}" />{L_LOCKED}</li>
</ul>

                [1] => 355
            )

    )

[3] => Array
    (
        [0] => Array
            (
                [0] => switch_legend
                [1] => 308
            )

        [1] => Array
            (
                [0] => switch_default
                [1] => 635
            )

    )

)

Expected Return 预期收益

Array
(
[0] => Array
    (
        [0] => Array
            (
                [0] => 
                [1] => 0
            )
        [1] => Array
            (
                [0] => switch_legend
                [1] => 11
            )
        [2] => Array
            (
                [0] => 
<ul id="picture_legend">
    <li><img src="{FORUM_NEW_IMG}" alt="{L_NEW_POSTS}" />{L_NEW_POSTS}</li>
    <li><img src="{FORUM_IMG}" alt="{L_NO_NEW_POSTS}" />{L_NO_NEW_POSTS}</li>
     <li><img src="{FORUM_LOCKED_IMG}" alt="{L_LOCKED}" />{L_LOCKED}</li>
</ul>

                [1] => 28
            )
        [3] => Array
            (
                [0] => switch_legend
                [1] => 308
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [0] => 
                [1] => 326
            )
        [1] => Array
            (
                [0] => switch_default
                [1] => 337
            )
         [2] => Array
            (
                [0] => 
<ul id="picture_legend">
    <li><img src="{FORUM_NEW_IMG}" alt="{L_NEW_POSTS}" />{L_NEW_POSTS}</li>
    <li><img src="{FORUM_IMG}" alt="{L_NO_NEW_POSTS}" />{L_NO_NEW_POSTS}</li>
     <li><img src="{FORUM_LOCKED_IMG}" alt="{L_LOCKED}" />{L_LOCKED}</li>
</ul>

                [1] => 355
            )
        [3] => Array
            (
                [0] => switch_default
                [1] => 635
            )
    )

)

What I need 我需要的

I would like the array to be clean from the matches, so that not each capture of the same pattern are in the same array rather each time it starts capturing it'll add to the same array like in the Expected Return Thanks. 我希望该数组在匹配中是干净的,以便不是每个相同模式的捕获都在同一个数组中,而是每次开始捕获时,它都将添加到同一数组中,如“ Expected Return感谢”中那样。

Sounds like PREG_SET_ORDER is what you want. 听起来像是您想要的PREG_SET_ORDER – mario –马里奥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM