这个PHP表达式有什么问题?

时间:2009-03-23 18:51:52

标签: php regex preg-match-all

有人可以向我解释为什么以下内容会返回空数组吗?

$reg = "/(\[{(false|true)};{.+};{\d}\])+/";
preg_match_all($reg,"[{false};{abcde};{10}][{true};{fghij};{10}]",$matches);
print_r($matches);

2 个答案:

答案 0 :(得分:2)

你写\d时应该是\d+

$reg = "/(\[{(false|true)};{.+};{\d+}\])+/";
preg_match_all($reg,"[{false};{abcde};{10}][{true};{fghij};{10}]",$matches);
print_r($matches);

虽然在你的情况下似乎并不重要,但我也会逃避括号,因为它们是特殊字符。

$reg = "/(\[\{(false|true)\};\{.+\};\{\d+\}\])+/";

答案 1 :(得分:2)

\ d应为\ d +为一个