I noticed that there are 3 different classes of quantifiers: greedy, lazy (i.e. non-greedy) and possessive.
I know that, loosely speaking, greedy quantifiers try to get the longest match by first reading in the entire input string and then truncate the characters one by one if the attempts keep failing; lazy quantifiers try to get the shortest match by first reading in the empty string and then add in the characters one by one if the attempts keep failing; possessive quantifiers try the same way as greedy quantifiers while they will stop matching if the first attempt fails.
However, I'm not sure how exactly the aboves are being implemented 'internally', and would like to ask for clarification (hopefully with examples).
For example, say we have the input string as "fooaaafoooobbbfoo"
.
If the regex is "foo.*"
(greedy), will the foo
in the regex first match the foo
in the input string, and then .*
reads in aaafoooobbbfoo
as 'the entire string'? Or will .*
first read in fooaaafoooobbbfoo
as 'the entire string', and then truncates fooaaafoooobbbfoo
to try matching the foo
in the regex? If it is the latter, will fooaaafoooobbbfoo
be truncated from its left or from its right in each attempt?
Will the answers to the above questions change if I replace "foo.*"
with ".*foo"
or "foo.*foo"
as my regex? What about if I change those greedy quantifiers to lazy ones and possessive ones?
And if there are more than one quantifiers in a regex, how will the engine deal with the priority (if that matters)?
Thanks in advance!