Sunday 24 November 2019

php - Why does this regex pattern not match?




Regex101 link: https://regex101.com/r/MsZy0A/2



I have the following regex pattern; .++b with the following test data; aaaaaaaacaeb.



What I don't understand is the "Possessive quantifier". I've read that it doesn't backtrack, which it normally does. However, I don't think it has to backtrack anyways? It only has to match anything up to and including "b", "b" would be matched twice, as .+ matches everything (including "b"), and the "b" after would also match "b".



Could someone please explain the possessive quantifier's role in this?



This question is not a duplicate of the one noted, I'm asking about this particular case because I still didn't get it after reading the other answer.



Answer



++ Matches between one and unlimited times, as many times as possible, without giving back - means, if you write .++, it matches everything including the final b. So the additional b in your regex will never matched.



You could get around this, if you don't use possessive quantifiers or simply remove the b from the matching class [^b]++b - but I would suggest the first. Possessive quantifiers are almost everytime unneccessary.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...