Results 1 to 2 of 2
Thread: Greedy Quantifier
- 08-25-2013, 07:02 PM #1
I have an application that takes a regualr expression and an input string to match with that pattern .For the following input and string
Enter your regex: .*foo // greedy quantifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.
The first example uses the greedy quantifier .* to find "anything", zero or more times, followed by the letters "f" "o" "o". Because the quantifier is greedy, the .* portion of the expression first eats the entire input string. At this point, the overall expression cannot succeed, because the last three letters ("f" "o" "o") have already been consumed. So the matcher slowly backs off one letter at a time until the rightmost occurrence of "foo" has been regurgitated, at which point the match succeeds and the search ends.
At this point, the overall expression cannot succeed, because the last three letters ("f" "o" "o") have already been consumed.
- 08-25-2013, 07:18 PM #2
- By sara12345 in forum Advanced JavaReplies: 2Last Post: 03-17-2010, 02:40 PM