3

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.

JordyvD
  • 1,565
  • 1
  • 18
  • 45
  • 3
    I have never seen such a pattern, but `(.+)+b` works just fine. So does `.+b`. – Tim Biegeleisen Dec 20 '17 at 08:42
  • Why would you have to +? What is the second + supposed to do? – Andreas Dec 20 '17 at 08:43
  • 6
    Since possessive quantifier doesn't backtrack `.++` matches everything including last `b` hence last `b` never matches – anubhava Dec 20 '17 at 08:45
  • @anubhava Thanks! That makes sense ️ – JordyvD Dec 20 '17 at 08:50
  • @Galen Not a dupe, I'm asking about this particular case =) – JordyvD Dec 20 '17 at 08:50
  • This [answer](https://stackoverflow.com/a/5319978/3540693) is amazing! Read it and you will understand it – Pentux Dec 20 '17 at 08:52
  • I actually read that one before posting this, it makes sense now, but my dumb brain didn't get it before hahahahaha – JordyvD Dec 20 '17 at 08:53
  • @g3mini I see. In that case, see [this answer](https://stackoverflow.com/a/9011951/8955448). It has a nice explanation of backtracking. Notice where it says backtracking would occur. This is what the possessive quantifier prevents, so the match fails. In your example, the `.++` immediately matches the whole `aaaaaaaacaeb`, so there is nothing left for the following `b` to match without backtracking. – Galen Dec 20 '17 at 08:55
  • In your example you have `.+` and this match with all the string. Then it try to match with the `b` but the matcher doesn't have more string to check because `.+` has match with all the string and doesn't remain more characters. Because the possessive quantifier doesn't backtrack. – Pentux Dec 20 '17 at 08:57

1 Answers1

3

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

Philipp
  • 15,377
  • 4
  • 35
  • 52
  • 2
    yeah but what do you do if there are one or more `b` in the middle of the string... I **don't** think that `[^b]++b` **will work properly**... Going for `.+b` would be better? – Allan Dec 20 '17 at 08:55
  • 1
    As wenn sonst know, what he actually want to schiebe, I cant tell you, whats really the best solution – Philipp Dec 20 '17 at 08:57
  • yeah that's why I voted +1 :-) – Allan Dec 20 '17 at 08:58
  • 1
    _Possessive quantifiers are almost everytime unneccessary_ citation needed. I don't agree with this, often found them (and atomic groups) very helpful. The rest of the answer is fine :) – Sebastian Proske Dec 20 '17 at 09:01