RegEx Gready vs Lazy

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.


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.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s