Use of greedy quantifier * in java?
Use of greedy quantifier * in java?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Mar 28, 2014, 6:52 PM
By default it matches the longest sequence of characters which fits the pattern, which is called 'greedy' behavior.
However, if you follow it with a ?, it matches the shortest sequence of characters which fits the pattern, which is called 'reluctant' (or 'lazy') behavior.
The following example illustrates the difference between the two kinds of behavior:
The output is:
When * is greedy it matches as many characters as possible until it finds a 't' and hence only finds the single sub-string : A stitch in t
However, when it's reluctant, it matches as few characters as possible until it finds a 't' and hence finds the 3 sub-strings above.
Abhijeet SinghPosted Mar 28, 2014, 11:03 PM