Comparison 5 min read

Greedy vs Lazy Regex: .* vs .*? Explained With Examples

Why .* matches too much and .*? stops early. Tested examples for quotes, tags and repeated letters, the negated-class alternative, and the dotAll flag.

greedy vs lazy regexregex non-greedyregex .*? meaninglazy quantifierregex match too much

The short answer

A greedy quantifier such as .* grabs as much text as it can and then gives back characters only if the rest of the pattern needs them. A lazy quantifier such as .*? grabs as little as possible. On <b>one</b> and <b>two</b>, the greedy pattern <b>.*</b> returns one long match covering both tags, while the lazy <b>.*?</b> returns two separate matches. Paste both into the Regex Tester with the g flag to see the difference highlighted.

Greedy and lazy quantifiers side by side

Adding ? after any quantifier makes it lazy.

GreedyLazyGreedy meaningLazy meaning
**?0 or more, as many as possible0 or more, as few as possible
++?1 or more, as many as possible1 or more, as few as possible
???0 or 1, prefer 10 or 1, prefer 0
{2,5}{2,5}?2 to 5, prefer 52 to 5, prefer 2

Do not confuse this ? with the ? quantifier itself. In colou?r the ? means "optional u". In .*? it modifies the * before it.

Example 1: quoted strings

Text: say "hi" and "bye"

PatternMatches (with g flag)
".*""hi" and "bye" (one match)
".*?""hi" and "bye"
"[^"]*""hi" and "bye"

The greedy version starts at the first quote, runs .* to the end of the line, then backs up until it finds a quote that lets the pattern finish. The last quote on the line is the first one it reaches when backing up, so the match spans both strings.

The lazy version starts at the first quote and extends one character at a time, stopping at the first closing quote that works.

Example 2: HTML-like tags

Text: <b>one</b> and <b>two</b>

  • Greedy <b>.*</b> matches <b>one</b> and <b>two</b> as a single match.
  • Lazy <b>.*?</b> matches <b>one</b> and <b>two</b>.
  • Negated class <b>[^<]*</b> also matches the two tags separately.

Regex is fine for quick searches like this in a known snippet, but it is not a reliable way to parse full HTML, where tags nest and attributes contain any characters. Use an HTML parser for real pages.

Example 3: watching a lazy quantifier step by step

On the text aaa:

  • a+ returns one match: aaa.
  • a+? with the g flag returns three matches: a, a, a. Each match takes the minimum of one character.
  • a{2,3}? returns aa. It takes the minimum of two and leaves the last a, which cannot form another match of at least two.

All three results were checked in JavaScript, the same engine the tester uses.

Lazy does not mean "shortest possible match"

This is the most common misunderstanding. A regex engine scans from left to right and returns the first position where a match succeeds. Lazy only affects how far the match extends from that starting position.

  • \w+? on abc123 returns a, because one character is the minimum.
  • \w+?$ on the same text returns abc123. The $ forces the match to reach the end, and since the engine found a match starting at position 0, it does not try later starting points that would give a shorter answer.

So if you need "the shortest quoted string anywhere", lazy quantifiers alone will not find it; they find the first one that works.

The negated character class is often better

For delimited text, "[^"]*" usually beats ".*?":

  • It is clearer. It says exactly what is allowed: anything except a quote.
  • It cannot overrun. A lazy .*? can still expand across delimiters if the rest of the pattern forces it to, while [^"]* simply cannot match a quote.
  • It can be faster. The engine does not need to try extending one character at a time and re-checking the rest of the pattern.

Use lazy quantifiers when the stop condition is more than one character, for example everything up to the word END: start(.*?)END.

Newlines and the dot

In JavaScript, . does not match a line break unless you add the s (dotAll) flag. This affects both greedy and lazy patterns:

  • <p>.*?</p> finds nothing in a paragraph that contains a line break.
  • The same pattern with the s flag matches across the line break.

We confirmed both results in Node.js. In the tester, type gs in the flags box to combine "all matches" with dotAll. A negated class such as [^<]* already matches newlines, which is another reason it is handy.

How to compare greedy and lazy in the Regex Tester

  • Open the Regex Tester.
  • Paste your text into the test string box.
  • Enter the greedy version of your pattern and put g in the flags box. Note how many matches are listed.
  • Add ? after the quantifier (for example change .* to .*?) and compare the highlighted matches.
  • Try the negated-class version too. If all three agree on your real data, prefer the negated class.
  • Matches are listed with the full matched text and any capture groups, so you can check exactly what (.*?) captured.

    Common mistakes

    • Making everything lazy "to be safe". ^.*?$ and ^.*$ match the same thing when anchors force the full line. Lazy only helps when something after the quantifier can stop it early.
    • Forgetting the g flag. Without it, the tester (like JavaScript's exec) shows only the first match, so the greedy/lazy difference may not be visible.
    • Expecting a lazy match at the end of a pattern to capture anything. start(.*?) captures an empty string, because nothing after the group forces it to grow. Add the closing delimiter: start(.*?)end.
    • Using lazy quantifiers in tools that do not support them. Basic POSIX tools such as plain grep (without -P) and sed do not understand *?. JavaScript, Python, PCRE, Java and .NET do.

    Using it in find and replace

    Lazy quantifiers are especially useful when replacing. To strip bold tags but keep the words, use the pattern <b>(.*?)</b> with the replacement $1 in the Find and Replace tool with "Use regex" ticked. A greedy .* would merge everything between the first opening and the last closing tag into one replacement. More capture-group recipes are in find and replace with regex, and the basics of quantifiers are in the regex beginner's guide.

    Frequently asked questions

    What does .*? mean in regex?

    It means any characters, zero or more times, but as few as possible. The ? after * turns the greedy quantifier into a lazy one, so the match stops at the first point where the rest of the pattern can succeed.

    Why does my regex match too much text?

    A greedy quantifier such as .* runs to the end of the line and then backs up only as far as needed, so it usually ends at the last possible delimiter. Use .*? or a negated class such as [^"]* to stop at the first one.

    Does a lazy quantifier find the shortest match?

    Not necessarily. The engine returns the first position where a match succeeds, and lazy only limits how far that match extends. For example \w+?$ on abc123 returns the whole abc123.

    Why does .*? not match across lines?

    In JavaScript the dot does not match line breaks unless you add the s (dotAll) flag. Add s to the flags, or use a negated class such as [^<]* which matches newlines.

    Try this tool on Lazyblink

    Put this guide into practice with our free online tool — no signup required.

    Open tool