Regular Expressions: Greedy vs Lazy Specifiers

Let’s say you want to match all href values in <a> tags in an HTML document. You might do something like

href=”.*”>

2

But that doesn’t work because the * is greedy and gets everything up until that last closing bracket on the first line. To fix this, make the * lazy but adding a question mark (?) after it.

href=”.*?”>

1

Notice now that we correctly get 3 matches instead of 2.