Quantifiers and Greed

Regular expression quantifiers are typically greedy. That is, when faced with a quantifier, the engine matches as much as it can while still satisfying the rest of the pattern. For instance:

preg_match("/(<.*>)/", "do <b>not</b> press the button", $match);
// $match[1] is '<b>not</b>'

The regular expression matches from the first less-than sign to the last greater-than sign. In effect, the .* matches everything after the first less-than sign, and the engine backtracks to make it match less and less until finally there’s a greater-than sign to be matched

Greedy and nongreedy quantifiers in Perl-compatible regular expressions

Greedy quantifierNongreedy quantifier
???
**?
++?
{m}{m}?
{m,}{m,}?
{m,n}{m,n}?
Greedy and nongreedy quantifiers in Perl-compatible regular expressions

Here’s how to match a tag using a nongreedy quantifier:

preg_match("/(<.*?>)/", "do <b>not</b> press the button", $match);
// $match[1] is "<b>"

Leave a Comment