Regex Greedy vs Lazy Matching
What is the difference between greedy and lazy regex matching? Greedy quantifiers grab as much text as they can; lazy ones grab as little as they can. One question mark switches between them, and picking wrong is the most common regex bug there is.
I once watched someone try to pull the text out of a single HTML bold tag with <b>.*</b> and get the entire page back. One pair of tags in, the whole document out. The fix was one character: <b>.*?</b>. That question mark is the difference between greedy and lazy matching, and it is the single most useful regex concept nobody teaches properly.
Greedy takes everything; lazy takes the minimum
By default, the quantifiers *, +, and {n,m} are greedy: they match as much text as possible while still letting the overall pattern succeed. Append a ? and they turn lazy, also called reluctant or non-greedy: *?, +?, {n,m}?, matching as little as possible.
The canonical example uses quoted strings. Take the text "first" and "second". The greedy pattern ".*" matches "first" and "second" as one match, swallowing the middle. The lazy pattern ".*?" matches "first" and "second" separately. In JavaScript: text.match(/".*"/g) gives ['"first" and "second"'], while text.match(/".*?"/g) gives ['"first"', '"second"']. Same pattern except for one character. Completely different result.
The HTML tag trap, and why everyone hits it
<div>Hello</div> and <div>World</div> with the greedy <div>.*</div> matches from the first <div> to the last </div>: the whole line, one match. The lazy <div>.*?</div> stops at the first closing tag and gives you two clean matches. This is the exact failure from the top of the article, and nearly every regex learner hits it within their first month, because greedy is the default and the default is wrong for delimited text.
Microsoft's .NET documentation has my favorite version of this. Extracting the last four digits from "1112223333 3992991999": the greedy \b.*([0-9]{4})\b skips straight to the end and captures only 1999. The lazy \b.*?([0-9]{4})\b captures 3333 and 1999. The engine was not broken in the first version. It did exactly what greedy means.
When lazy backfires
Lazy is not "the correct mode." It is a tool with its own failure cases. On nested structures, lazy stops at the first delimiter, which is wrong when the content itself contains delimiters. Matching <div>...</div> lazily across nested divs gives you the inner close, not the outer one. No quantifier mode parses nested markup. That is a parser's job.
Lazy can also be slower, since the engine expands the match one character at a time and retries the rest of the pattern at each step. And in POSIX regex engines, the ? modifier does not exist at all: no lazy matching, period. If your regex runs in a POSIX-only context, you need a different strategy, like a negated character class ([^"]* instead of ".*?").
My rule: default to lazy when extracting delimited content, default to greedy when consuming everything up to a final delimiter or the end of the string. I still reach for greedy first when skimming a log file, because I want the rest of the line, and I still get burned by it in HTML about once a year. Old habits, wrong defaults. And when a pattern starts behaving exponentially, the quantifier mode is rarely the real problem. That is catastrophic backtracking, which has its own article on this site.
Frequently asked questions
How do I make a regex quantifier lazy?
Append ? to the quantifier: *?, +?, {n,m}?. The ? after a quantifier switches it from greedy (match as much as possible) to lazy (match as little as possible).
Which regex quantifiers are greedy by default?
*, +, ?, and {n,m} are all greedy by default in most engines, including JavaScript, Python, .NET, and PCRE.
Why does my regex match too much text?
Almost always a greedy quantifier spanning repeated delimiters, like .* between two quotes. Try the lazy version, .*?, first.
Is lazy matching slower than greedy?
It can be: the engine expands one character at a time and retries the rest of the pattern at each step. For most everyday patterns the difference is negligible.
Do all regex engines support lazy quantifiers?
No. POSIX BRE and ERE engines have no lazy mode. In those engines, use a negated character class like [^"]* instead of ".*?".
Related reading: Catastrophic Backtracking: The Regex That Took Down Cloudflare for 27 Minutes · Regex Lookahead and Lookbehind Explained: The Zero-Width Trick · Why Most Email Regex Patterns Are Wrong (And What to Use Instead)
One practical regex guide a week. Get it by email.