Reputation: 2052
I have this subpattern:
<?php
$wavs = 'aaaa="" wav="d" bbbbb="" wav="gerg" ccccc="" wav="" ddddd=""';
preg_match_all('#(?<=wav=").+?(?=")#', $wavs, $matches);
print_r($matches);
?>
It results in this output:
php test.php
Array
(
[0] => Array
(
[0] => d
[1] => gerg
[2] => " ddddd=
)
)
While I expected just 2 matches:
php test.php
Array
(
[0] => Array
(
[0] => d
[1] => gerg
)
)
What is the issue here? Why extra unrelated string is being captured?
EDIT: (M42 response)
preg_match_all('#(?<=wav=").*?(?=")#', $wavs, $matches);
still results in incorrect matches:
Array
(
[0] => Array
(
[0] => d
[1] => gerg
[2] =>
[3] => " ddddd=
)
)
EDIT: (Sniffer)
OMG YES! THANK YOU SIR! EXACTLY WORKS!
preg_match_all('#(?<=wav=")\w+?(?=")#', $wavs, $matches);
Array
(
[0] => Array
(
[0] => d
[1] => gerg
)
)
Upvotes: 2
Views: 124
Reputation: 19423
What is the issue here? Why extra unrelated string is being captured?
#(?<=wav=").+?(?=")#
^^^
This is the reason, it matches everything including the space and the "
You probably wanted:
#(?<wav=")\w+(?=")#
Upvotes: 5
Reputation: 91415
Change the +
modifier to *
:
preg_match_all('#(?<=wav=").*?(?=")#', $wavs, $matches);
// __^
or
preg_match_all('#(?<=wav=")[^"]+(?=")#', $wavs, $matches);
Upvotes: 3
Reputation: 89557
to solve the issue you can replace .+?
by [^"]+
, example:
preg_match_all('#(?<=wav=")[^"]+(?=")#', $wavs, $matches);
Upvotes: 3