?
In the example below, we create an expression to match all HTML span elements. The greedy version of the example produces a very different — and unexpected — result.
*
Using the
Greedy quantifier
Greedy
*?
Using the
Non-greedy quantifier
Non-greedy
one <span>two</span> three <span>four</span> five
one <span>two</span> three <span>four</span> five
"<span>.*</span>"
"<span>.*?</span>"
1
2
1
<span> two</span> three <span>four </span>
1
<span> two </span>
2
<span> four </span>
The greedy quantifier looks for the longest match. The non-greedy (lazy) quantifier finds the shortest match.