Bulk Replace
Reference

Greedy vs Lazy Regex Quantifiers Explained With Real Examples

Why .* grabs too much and how .*? fixes it. Greedy and lazy quantifiers explained with HTML and log examples, plus a faster negated-class alternative.

Updated 5 minute read The Bulk Replace team

A greedy quantifier such as * or + matches as much text as it can, then gives back only what the rest of the pattern needs. A lazy quantifier, written with an extra question mark (*?, +?), matches as little as it can and only takes more if the pattern would otherwise fail. That is why <.*> on <b>bold</b> matches the whole string, while <.*?> matches just <b>. In find and replace, greedy patterns are the most common cause of "it replaced far too much". Often the best fix is neither: a negated class like <[^>]*> is clearer, faster and cannot overrun.

This guide shows the difference with real HTML and log examples, explains a few behaviors that surprise people, and gives a rule of thumb for choosing.

The quantifiers side by side

GreedyLazyMeans
**?Zero or more
++?One or more
???Zero or one
{2,5}{2,5}?Between 2 and 5
{2,}{2,}?2 or more

Lazy versions exist in every engine you are likely to use on Windows: .NET (PowerShell), JavaScript (VS Code) and Boost (Notepad++). They match the same set of strings as their greedy twins. The only difference is which match the engine prefers when more than one is possible.

Example 1: HTML tags

Text: <b>bold</b> and <i>italic</i>

PatternFirst match
<.*> (greedy)<b>bold</b> and <i>italic</i>
<.*?> (lazy)<b>
<[^>]*> (negated class)<b>

The greedy .* runs to the end of the line, then backtracks one character at a time until it finds a >, which is the very last one. Replace that match with nothing and you delete the whole line. The lazy version stops at the first >. The negated class says "anything except >", so it cannot pass one. See regex to remove HTML tags for the full set of tag patterns.

Example 2: content between tags

Text: <p>One</p><p>Two</p>

  • <p>(.*)</p> captures One</p><p>Two.
  • <p>(.*?)</p> captures One, and a second match captures Two.

Here a negated class does not work as neatly, because the end marker is several characters long. The lazy quantifier is the right tool. The same idea, extended across line breaks with [\s\S]*?, is covered in matching everything between two strings.

Example 3: log lines

Text: user=alice id=42 status=ok

PatternGroup 1
user=(.*) alice id=42 (to the last space)
user=(.*?) alice
user=(\S+)alice

Another log line: ERROR disk full: C: at 10:15. ERROR (.*): captures disk full: C: at 10 because there are three colons and the greedy version uses the last. ERROR (.*?): captures disk full. When the text contains several copies of your end character, greedy picks the last and lazy picks the first.

Three things that surprise people

Lazy does not mean "shortest match anywhere"

The engine still starts at the leftmost position where a match is possible. a.*?b on aaab matches aaab, not ab, because a match starting at the first a is found before the engine ever tries the third. Lazy only controls where the match ends.

A lazy quantifier at the end of a pattern matches almost nothing

\d+? on 123 matches just 1, because nothing after it forces it to take more. Lazy quantifiers only make sense when something follows them: \d+?def on 123def matches 123def.

Greedy patterns can be slow as well as wrong

Each time a greedy .* overshoots, the engine backtracks character by character. On long lines or whole files read as one string, that adds up. Nested quantifiers such as (a+)+b can take an extremely long time on text that almost matches, a problem known as catastrophic backtracking. .NET lets a program set a time limit for this, and some tools stop a search that runs too long.

The negated class: often the best choice

When the end of what you want is marked by a single character, a negated class beats both quantifier styles:

  • Quoted text: "[^"]*" rather than ".*?"
  • A tag: <[^>]*> rather than <.*?>
  • Up to the end of a field: [^,]* in CSV, [^&]* in a query string
  • Up to the end of the line: [^\r\n]*

Why it is better:

  1. Clarity. [^"]* says exactly what it means: everything up to the next quote.
  2. Speed. It never overshoots, so there is nothing to backtrack.
  3. Safety. It cannot run past the delimiter even if the rest of the pattern fails, so it will never stretch a match across two values.

One difference to remember: a negated class matches line breaks unless you exclude them, while . does not by default. "[^"]*" can span lines; "[^"\r\n]*" cannot.

Rule of thumb

SituationUse
End marked by a single characterNegated class, such as [^"]*
End marked by a word or tagLazy, such as .*?</p>
Really do want "up to the last one"Greedy, such as .*\. for everything up to the last dot
Rest of the lineGreedy .* or [^\r\n]*; both stop at the line break

Greedy is not wrong; it is the default because it is often what you want. (.*)\.(\w+)$ on a file name puts everything up to the last dot in group 1 and the extension in group 2, so archive.tar.gz splits into archive.tar and gz.

A third option: atomic and possessive

Some engines offer a quantifier that is greedy and never gives anything back. It does not change what you can match so much as how quickly a failed match gives up:

  • Atomic groups, (?>...), are supported in .NET (PowerShell) and Boost (Notepad++). Once the group has matched, the engine will not backtrack into it.
  • Possessive quantifiers, such as *+ and ++, are shorthand for the same idea in Boost and PCRE. .NET does not support them.
  • JavaScript, and therefore VS Code, supports neither.

You rarely need these for everyday find and replace. A negated class usually gives the same protection and works everywhere.

Spotting the problem before it spreads

A greedy pattern often looks fine on a test line that contains one target and goes wrong on real lines that contain two. Test on a line with several matches, and in a bulk run, check the longest match, not just the count. Bulk Replace shows a before/after diff for every match in every file, which makes an over-long greedy match easy to spot, and it backs up each run so one click undoes it. For the full symbol list, keep the regex cheat sheet nearby; Bulk Replace accepts every pattern on it.

Frequently asked questions

What does lazy mean in regex?

A lazy quantifier like *? or +? matches as few characters as possible, then expands only if the rest of the pattern fails.

Is a lazy quantifier always better?

No. A negated class such as [^"]* is often clearer and faster, because it cannot run past the character you want to stop at.

Why does my pattern match from the first tag to the last tag?

The .* is greedy and runs as far as it can. Use .*? or a negated class like [^<]* so it stops at the first closing tag.

Get three guides like this every month, plus software update notices.Subscribe →