0

我的正则表达式有一个错误,有人为我写的。我试图编写自己的正则表达式,但这对我来说很难破解。

我有一个自定义“标签”,例如 {module:agenda:getlist:params(2)} 但是.. 如果我只有 1 个参数,则正则表达式看不到 2,但得到了错误的代码。

这是我的正则表达式代码

$paramcount = preg_match_all('{
        # [^,]+ everything that isn't a comma
        # (?<=...) is a look behind. Meaning that the part that has the 3 dots 
        # becomes matched but not goes through the rest of the regex

        # matches "null" in "params(null"
        (?<=params\()[^,]+

        | # this is the separation dash

        # (?=...) is a look ahead, same as the look behind but than on the end

        # matches "null" in " null)"
        (?<= )[^,]+(?=\))

        |

        # matches "true" in ", true" and ""foo"" in ", "foo""
        (?<=, )[^,]+
    }x', $data, $parammatches);

所以当我尝试匹配它时..结果是:

$array = array
    (
        [0] => params(2
    );

这不是我想要的,我只希望 2 匹配而不是其余的。当我给它更多像这样“params(null, 2)”的参数时,一切正常,我有 2 个不错的数组值。

有人可以在这里指导我或帮助我吗?

编辑:可以在 pastebin 上找到更多输出。 http://pastebin.com/x0WL9K4u

4

1 回答 1

0

有人回答了我的问题。他为我的正则表达式问题写了一个更新

<?php
$str = 'params(null, true, "Foo", null)';

preg_match_all('{
# [^,]+ betekend alles wat geen komma is
# (?<=...) is een look behind. Dit betekend dat het gedeelte op de 3 puntjes
# gematched moet worden voor de rest van de regex, maar dat hij niet meegenomen
# wordt in de match

# matches "null" in "params(null"
(?<=params\()[^,]+

| # dit is het scheidingsteken

# (?=...) is een look ahead, zelfde als look behind maar dan voor erachter

# matches "null" in " null)"
(?<= )[^,]+(?=\))

|

# matches "true" in ", true" and ""foo"" in ", "foo""
(?<=, )[^,]+
}x', $str, $matches);

$params = $matches[0];
?>
于 2013-08-21T11:59:53.780 回答