我有这个子模式:
<?php
$wavs = 'aaaa="" wav="d" bbbbb="" wav="gerg" ccccc="" wav="" ddddd=""';
preg_match_all('#(?<=wav=").+?(?=")#', $wavs, $matches);
print_r($matches);
?>
导致此输出:
php test.php
Array
(
[0] => Array
(
[0] => d
[1] => gerg
[2] => " ddddd=
)
)
虽然我预计只有2场比赛:
php test.php
Array
(
[0] => Array
(
[0] => d
[1] => gerg
)
)
这是什么问题?为什么要捕获额外不相关的字符串?
编辑:(M42响应)
preg_match_all('#(?<=wav=").*?(?=")#', $wavs, $matches);
仍会导致错误匹配:
Array
(
[0] => Array
(
[0] => d
[1] => gerg
[2] =>
[3] => " ddddd=
)
)
编辑:(嗅探器)
哦,是的!谢谢你,先生!确切的工作!preg_match_all('#(?<=wav=")\w+?(?=")#', $wavs, $matches);
Array
(
[0] => Array
(
[0] => d
[1] => gerg
)
)
答案 0 :(得分:5)
这是什么问题?为什么要捕获额外不相关的字符串?
#(?<=wav=").+?(?=")#
^^^
This is the reason, it matches everything including the space and the "
你可能想要:
#(?<wav=")\w+(?=")#
答案 1 :(得分:3)
将+
修饰符更改为*
:
preg_match_all('#(?<=wav=").*?(?=")#', $wavs, $matches);
// __^
或
preg_match_all('#(?<=wav=")[^"]+(?=")#', $wavs, $matches);
答案 2 :(得分:3)
要解决此问题,您可以.+?
替换[^"]+
,例如:
preg_match_all('#(?<=wav=")[^"]+(?=")#', $wavs, $matches);