-1

i have read so many questions and answers on possessive quantifiers in java in this website and java tutorials but men, am still confused!! am not understanding!! for example lets say

 my regex is .*+foo
 my input string is mdfoo

according to my understanding,

.(dot) means there are other characters before foo

*(star) means if foo should be writen in a string form if found by regex

+foo means that a string should end with foo for it to match

and in my input string,it ends with foo. but when i receive a statement that there was no match when i run my program! how is this happening and what should i do still using the (+) quantifier to receive a match ?. thanks in advance

Edijae Crusar
  • 3,105
  • 2
  • 32
  • 66
  • Besides the one I already mentioned, here are more: http://stackoverflow.com/questions/1117467/can-someone-explain-possessive-quantifiers-to-me-regular-expressions and http://stackoverflow.com/questions/6485958/understanding-possessive-quantifiers-java-regex and even [more](https://www.google.com/search?q=possessive+quantifiers+stackoverflow)... – Bart Kiers Jun 02 '14 at 14:28
  • @Bart Kiers okey, i got it sir. i will read all of them – Edijae Crusar Jun 02 '14 at 17:26

2 Answers2

0

The possessive quantifier implies that no backtracking is done.

What happens when you try to match mdfoo against .*+foo:

  1. The first part of the pattern (i.e. .*+) matches the whole string mdfoo
  2. But the second part of the pattern (i.e. foo) is not found after the first match
  3. Since there is no backtracking, the attempt immediately fails

These possessive quantifiers are quite clearly explained here.

sp00m
  • 44,266
  • 23
  • 127
  • 230
0

There are three types of quantifiers:

  • the "regular" quantifiers (*, +, ?) also called "greedy" quantifiers;
  • the "lazy", quantifiers (*?, +?, ??);
  • the "possessive" quantifiers (*+, ++, ?+).

For instance, take this input:

The answer is 42

Now, take this regex:

.*(\d+)

The question is, what will be captured by (\d+) according to which version of * you use in .*:

  • if *, what will be captured is 2;
  • if *?, what will be captured is 42;
  • if *+, the regex does not match.

Why:

  • the greedy quantifier swallows everything it can; however, it retains, on its way, the positions where it has matched; when having swallowed the full text, there is still \d+ to match; it will reluctantly backtrack until \d+ is satisfied, and \d+ is satisfied with 2;
  • the lazy quantifier tries and queries the following regex token: "if \d+ does not match then I swallow the next character"; when encountering 4 it lets \d+ do its job, therefore 42 is captured;
  • the possessive quantifier is possessive; it works like the greedy quantifier except that it does not retain any positions; therefore, when reaching the end, the regex engine asks "OK, can you give back?", .*+ says "No...", therefore no match.
fge
  • 110,072
  • 26
  • 223
  • 312