clear understanding on possesive quantifiers

This question already has an answer here:

  • Greedy vs. Reluctant vs. Possessive Quantifiers 7 answers

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

  • The possessive quantifier implies that no backtracking is done.

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

  • The first part of the pattern (ie .*+ ) matches the whole string mdfoo
  • But the second part of the pattern (ie foo ) is not found after the first match
  • Since there is no backtracking, the attempt immediately fails
  • These possessive quantifiers are quite clearly explained here.

    链接地址: http://www.djcxy.com/p/76906.html

    上一篇: 所有格量化符正则表达式的实际使用

    下一篇: 清晰的认识量化值