Chekout http://www.regular-expressions.info/repeat.html .
Greed refers to the number of times a regex engine will try to match a specific character set. The regular expression expression greed method uses special characters * , + ? and {} .
Consider
str = "asdfasdfbbbb" r1 = /b/ r2 = /(asdf)*/ r3 = /b{3}/ r4 = /.*/
Matching this regular expression from str will result in:
r1 matching "asdfasdf b bbb" (not greedy, trying to combine b only once)
r2 matching <asdfasdf bbbb "(greedy, trying to combine asdf as many times as possible)
r3 match "asdfasdf bbb b" (not greedy, match b exactly 3 times)
r4 matching < asdfasdfbbbb "(ULTRA-greedy, matches almost any character as many times as possible)
Since regex means representing specific text patterns, it doesn't look like greed, it's an issue of approach. Sometimes you will need to match three times foo ( /(foo){3}/ ) or an infinite score ( /(bar)*/ ).
source share