3

I was looking at this question Greedy vs. Reluctant vs. Possessive Quantifiers

I can see how *+ and * both match zero or more times, but the possessive quantifier *+ will match forwards as much as possible.. And the * will do .* and backtrack. And I can accept that *+ would be more efficient when the .* string is long.

I'm interested in when they give different results though.

And I saw a comment

@moodboom, there are zero cases ever (mathematical fact) where possessive quantifiers will produce a match that will not be produced by simple greedy quantifiers. There are occasional cases where they will produce a no match when greedy quantifiers would produce a match. For ALL other cases (where greedy and possessive produce the same results), possessive quantifiers give a performance gain. – Wildcard May 5 at 23:00

I'd be very interested to see this expanded upon, specific cases where possessive and greedy quantifiers give a different result.

Contrasting *+ and *

I'd also be interested in the case of what different results are possible, contrasting ?+ vs ?

barlop
  • 12,887
  • 8
  • 80
  • 109
  • Hmmm, I meant to answer this but didn't get around to it. On mobile now but may be able to do so tomorrow. – Wildcard Aug 08 '17 at 08:43
  • @Wildcard thanks. whenever you have time is fine – barlop Aug 08 '17 at 09:49
  • Very related (with an example of the different matches, and with many beautiful explanations of the intricacies in the answers): https://stackoverflow.com/questions/5319840/greedy-vs-reluctant-vs-possessive-quantifiers – Dewi Morgan Aug 18 '17 at 14:33
  • 1
    @DewiMorgan I mention that one in the first line, that is useful as a foundation to help understand this question but that question is quite general and the answers there don't cover this question. – barlop Aug 19 '17 at 07:52

2 Answers2

2

I found a case, yet I'm not certain about the explanation and pertinence. And I think there are many other cases.

Test case

greedy = /.*b/
posssessive = /.*+b/

Tested on:

foob

Only greedy matches it.

Explanation

Possessive will first match the whole string (.*+) and then try to match b char but find only end of string ($).

Greedy will also match the whole string, but then look backward until it finds a first b char. Which it will find.

barlop
  • 12,887
  • 8
  • 80
  • 109
Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
  • You say you tested it on foo and foob. Are you OK with me simplifying your answer by removing your example of `foo` but keeping the one of `foob` the reason being that both the greedy and the possessive give the same result for foo (not matching it), so foo is irrelevant. What is relevant is your second example - foob. Your answer would be simpler and clearer without the 'foo' example 'cos foo isn't really an example of what I asked for. 'foob' on the other hand is. – barlop Aug 18 '17 at 09:42
  • UPVOTED Your finding re foob is amazing, such a simple test case too. – barlop Aug 18 '17 at 09:43
  • I agree with you, in fact when I finished writing the article I wondered about deleting the first example. Is it really what you expected from an answer though ? I wasn't sure while writting it. – Ulysse BN Aug 18 '17 at 14:02
  • 1
    I'm glad you deleted the non-example, and yes your answer answers the question and thus far it is the only answer to the question(alpha bravo's attempt at an answer shows he didn't even understand the question). And while yes i'm sure if the right person stopped by then i'd get an answer that might go in more depth but this is the only answer so far. – barlop Aug 19 '17 at 08:47
0

check out the examples in the demo for test strings aaab and aaax try the greedy pattern a*[^b] and the possessive pattern a*+[^b]

Demo 1

Demo 2

a*[^b] will backtrack to try to find a match hence it finds aaa in aaab

a*+[^b] will find aaa will not backtrack and will try to match [^b], fails on aaab

alpha bravo
  • 7,838
  • 1
  • 19
  • 23
  • I asked for examples where the result is **different** You've just described both as finding 'aaa' that sounds like the same result. I am aware that the process is different but I asked for examples with different results. You also haven't covered the last line of what I wrote either. – barlop Aug 03 '17 at 22:58