php > preg_match("@/m(/[^/]+)+/t/[email protected]", "/m/part/other-part/t", $m);
php > var_dump($m);
array(2) {
[0]=>
string(20) "/m/part/other-part/t"
[1]=>
string(11) "/other-part"
}
php > preg_match_all("@/m(/[^/]+)+/t/[email protected]", "/m/part/other-part/t", $m);
php > var_dump($m);
array(2) {
[0]=>
array(1) {
[0]=>
string(20) "/m/part/other-part/t"
}
[1]=>
array(1) {
[0]=>
string(11) "/other-part"
}
}
与上述的例子,我想捕获同时匹配/part
和/other-part
,不幸的正则表达式/m(/[^/]+)+/t/?
不同时捕获,如我所料。正则表达式量化捕获
该捕获不应仅限于匹配此样本,它应该捕获捕获组的重复次数的未定义数量;例如/m/part/other-part/and-another/more/t
UPDATE: 鉴于这是预期的行为,我的问题代表作为我将如何能够实现我的这个匹配?
我预计是由于量词反复应用的捕获组'+' – mhitza 2012-03-14 14:28:49
也“不工作”是不是好有一个问题标题。实际上,量化器确实如PCRE中所公布的那样工作。 – hakre 2012-03-14 14:31:37
@hakre从标题中删除了不工作的部分。根据你的回答,我的记忆似乎在欺骗我,因为我记得对捕捉组进行了量化。 – mhitza 2012-03-14 14:33:57