4

I understand .* (greedy quantifier) backtracks and tries to find a match. and .*+ (possessive quantifier) does not backtrack.

However I have been using .* and .\*? frequently but don't know when to use .*+.

Can somebody give a situation or an example where .*+ should be used?

explanation with an example is appreciated.

EDIT:

i have gone through the theory part and i repeat i understand how it works. i just need one example that matches possessive quantifiers (.*+)

Arun Gowda
  • 2,721
  • 5
  • 29
  • 50
  • https://www.regular-expressions.info/possessive.html – Robin Mackenzie Jan 19 '18 at 11:52
  • For those marking posssible duplicate, Please note that i understand how they work. I just can't make out an example / practical use of possessive quantifiers – Arun Gowda Jan 19 '18 at 11:57
  • [this answer](https://stackoverflow.com/a/48209111/1454708) uses a possessive quantifier to fix regex (it is the same as an atomic group around) – Nahuel Fouilleul Jan 19 '18 at 12:15
  • @ArunGowdru The "Possible Dup" contains an example of `.*+`, and proceeds to contrast it with other examples, in depth. Is that what you wanted? – jpaugh Jan 19 '18 at 20:00
  • Okay. I came across one today. https://stackoverflow.com/questions/50286545/capture-stream-of-digits-which-is-not-followed-by-certain-digits/50286663#50286650 – Arun Gowda May 11 '18 at 08:47

2 Answers2

2

There are many (regex dependent) implementation details so it's difficult to generalize these things. For example with ^.*.+ you get a match on the string " ". With ^.*+.+ you don't. Because the first matcher already swallowed the whole string of whitespace.

You can use it in any case where you don't want the next part of your regex to accidentally match a part of the preceding.

You can test this with PCRE settings at https://regex101.com/

CodeMonkey
  • 4,067
  • 1
  • 31
  • 43
  • Could you please give an example that matches .*+ ? so that i can work it out ? – Arun Gowda Jan 19 '18 at 11:59
  • An empty string or any number of whitespaces will match that. You won't notice a difference to `.*` – CodeMonkey Jan 19 '18 at 12:11
  • 1
    This is wrong. Both of the regular expressions here will give the same result. The first part, `.*` or `.*+`, will match the entire string. The final `.*` will then have nothing to match, but it doesn't *need* to match anything. – Yay295 May 28 '19 at 22:08
  • 1
    @Yay295 Thank you for pointing that out, I wrote the regexes wrong. I fixed the answer. – CodeMonkey May 29 '19 at 06:56
1

Note that if a possessive patter matches, then so will the greedy pattern. The converse is not true. Therefore you can use a possessive quantifier if you want to limit your matches to a smaller set.

Secondly possessive quantifiers are useful when the input string does not match your pattern. Since they "eat" their input and don't backtrack they will be faster in detecting a non-match. In extreme cases this is called catastrophic backtracking and has brought down sites (including StackOverflow, see here).

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137