gpt4 book ai didi

php preg_match_all 匹配错误的字符串

转载 作者:行者123 更新时间:2023-12-04 15:31:47 28 4
gpt4 key购买 nike

我有这个子模式:

<?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
)

)

最佳答案

这里有什么问题?为什么会捕获额外的无关字符串?

 #(?<=wav=").+?(?=")#
^^^
This is the reason, it matches everything including the space and the "

你可能想要:

#(?<wav=")\w+(?=")#

关于php preg_match_all 匹配错误的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19430696/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com