Regex Non-greedy (or Lazy)

Summary: in this tutorial, you’ll learn about the regex non-greedy (or lazy) quantifiers that match their preceding elements as few times as possible.

Introduction to the regex non-greedy (or lazy) quantifiers

In regular expressions, the quantifiers have two versions: greedy and non-greedy (or lazy). In the previous tutorial, you learned how greedy quantifiers work.

To turn a greedy quantifier into a non-greedy quantifier, you can append a question mark (?) to it. The following table shows the greedy and non-greedy quantifiers:

Greedy quantifierLazy quantifierMeaning
**?Match its preceding element zero or more times.
++?Match its preceding element one or more times.
???Match its preceding element zero or one time.
{ n }{ n }?Match its preceding element exactly n times.
{ n ,}{ n ,}?Match its preceding element at least n times.
{ n , m }{ n , m }?Match its preceding element from n to m times.

The following example uses the non-greedy quantifier (+?) to match the text within the quotes (""):

<?php

$str = '<a href="/" title="Go to homepage">Home</a>';

$pattern = '/".+?"/';

if (preg_match_all($pattern, $str, $matches)) {
    print_r($matches[0]);
}Code language: HTML, XML (xml)

Output:

Array
(
    [0] => "/"
    [1] => "Go to homepage"
)
Code language: PHP (php)

How the non-greedy quantifier (+? ) works.

First, the regex engine searches for a match starting from the first position in the string. Because the regex engine cannot find a match, it continues searching until it finds the first quote ("):

Second, the regex engine finds a match for the rule .+? . However, the non-greedy quantifier +? just matches one time, not multiple times to the end of the string like the greedy quantifier (+). And the regex engine immediately looks at the next rule in the pattern, which matches a quote (“):

Third, the regex engine repeats the first and second steps starting from the position after the first match and returns the next result:

Finally, the regex engine continues to search till the end of the string:

Summary

  • Append a question mark (?) to a quantifier to turn it into a non-greedy quantifier.
  • A non-greedy quantifer tries to match its preceding element as few times as possible.
Did you find this tutorial useful?