Regex未完全工作


Regex not fully working

我在页面中使用了一个特殊的标记。这个标签是这样组成的;

输入:{{ module : menu : params(mainmenu , index) }}

现在我遇到的问题是我的正则表达式没有读取第二个参数,所以我无法访问这个参数。

这是我的标签,我有{{module:menu:params(mainmenu, index)}} 的问题

$str = 'params(null)';
preg_match_all('{
# matches "null" in " null)"
(?<='s)[^,]+(?='))
|
# matches "null" in "(null)"
(?<='()[^,]+(?='))
|
# matches "null" in "params(null"
(?<=params'()[^,]+
|    
# matches "true" in ", true" and ""foo"" in ", "foo""
(?<=,'s)[^,]+
}x', $str, $matches);

现在regex开始工作,只给我1个值,介于带params的钩子之间。

PHP给我的数组是:

$array = array
(
[0] => module
[1] => menu
[2] => params(mainmenu, index)
);

regex是用来匹配params的,所以它给了我所有输入到标记中的params。

输出现在只读取第一个参数,它给出了这个结果;

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

需要/预期输出但是我想要/需要这个

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

我希望我的解释足够好,如果不是,请评论,这样我可以把它说得更清楚。

您可以尝试以下regex来获得mainmenuindex字符串的数组,

(?<='()[^,]*|[^') ]+(?='))

演示

代码:

<?php
$data = " {{module:menu:params(mainmenu, index)}}";
$regex =  '~'w+[^,'(]+(?=[^()]*'))~';  // this is the solution that works
preg_match_all($regex, $data, $matches);
print_r($matches);
?>

输出:

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