Lets say we have this string
John said, "What are you doing"
We want to get only the style sheet and not the rest of the contents. How about if we use this?
Regular Expression: style=".+"
This will match John said, "What are you doing"
What about if we use
Regular expression : style=".*"
This will again match from start of style=" ........ to the end of quote in the same line.... "
This is called Greedy Algorithm
Regular Expression Greedy
It returns the whole line until the last character is reached.
Regular Expression Lazy
Lazy search means it does not want to go that far. It will return the first match of pattern. Example is below
Regular Expression: style=".+?"
This will return style="color: red;". Notice ? after +. This basically says do Lazy search or not greedy to return the first match.
Summary
John said, "What are you doing"
style=".*", when matches style="color: red;", it does not stop there. It continues to find more matches or rather a bigger match. It goes till the very hand of the line, then it back tracks. When it find the last " then it stops.
style=".*?", when matches style="color: red;", stops there and does not search anymore. This returns the shortest string.
In nutshell, Lazy return the smallest string, greedy returns the largest string.