问题
I am trying to practice asterisk * quantifier on a simple string, but while i have only two letters, the result contains a third match.
<?php
$x = 'ab';
preg_match_all("/a*/",$x,$m);
echo '<pre>';
var_dump($m);
echo '</pre>';
?>
the result came out:
array(1) {
[0]=>
array(3) {
[0]=> string(1) "a"
[1]=> string(0) ""
[2]=> string(0) ""
}
}
As i understand it first matched a then nothing matched when b, so the result should be
array(1) {
[0]=>
array(2) {
[0]=> string(1) "a"
[1]=> string(0) ""
}
}
So what is the third match?
回答1:
From using a regex demo tool here, we can see that the first match is a
, while the second and third matches are the zero width delimiters in between a
and b
, and also in between b
and the end of the string.
Keep in mind that the behavior of preg_match_all
is to repeatedly take the pattern a*
and try to apply it sequentially to the entire input string.
I suspect that what you really want to use here is a+
. If you examine this second demo, you will see that with a+
we only get a single match, for the single a
letter in ab
. So, I vote for using a+
here to resolve your problem.
回答2:
Your regular expression '/a/*' Matches zero(empty) or more consecutive a
characters.
Example : if you try to match '/a*/' to an empty string it will return one match because * refer to nothing or more . see here
the preg_match_all continues to look until finishning processing the entire string. Once match is found, it remainds of the string to try and apply another match.
来源:https://stackoverflow.com/questions/54419455/strange-result-of-using-asterisk-quantifier