我的正则表达式中有错误,有人为我写信。 我试着编写自己的正则表达式,但这对我来说很难破解。
我有一个自定义的“标签”,如{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
答案 0 :(得分: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];
?>