Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this regex pattern not match? [duplicate]

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.

like image 804
JordyvD Avatar asked Feb 01 '26 04:02

JordyvD


1 Answers

++ 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.

like image 85
Philipp Avatar answered Feb 02 '26 16:02

Philipp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!