Your pattern was supposed to pull out the first quoted phrase. Instead it returned half the sentence, starting at the first quote mark and ending at the last one, swallowing everything in between. Nothing is broken. The quantifier did what quantifiers do by default: it took as much as it could. That behaviour is called greedy, and its opposite, lazy, is one question mark away.
This guide runs the same pattern both ways on the same test string in the free Regex Tester on Ovatly, shows how the difference appears in the match list, and works through when each behaviour is the one you actually want.
Contents
- Quick answer
- What greedy actually does
- What lazy actually does
- The same pattern both ways, step by step
- Worked example: two bold phrases
- When greedy is the right choice
- When lazy is the right choice
- Limitations worth knowing
- Common mistakes
- FAQs
- Conclusion
Quick answer
A greedy quantifier (.*, .+, a{2,5}) matches as much text as it can, then backtracks if the rest of the pattern fails. A lazy quantifier (.*?, .+?, a{2,5}?) matches as little as it can, then extends if the rest of the pattern fails. On the test string "<b>bold</b> and <b>bright</b>", the pattern "<b>.*</b>" returns one match: the entire string. The pattern "<b>.*?</b>", run with the g flag in the Regex Tester, returns two matches: "<b>bold</b>" and "<b>bright</b>". Same string, same delimiters, opposite appetite.
What greedy actually does
Greedy is the default. When the engine reaches .*, it consumes the rest of the string in one gulp. Then it tries to match whatever comes next in the pattern. If that fails, it gives back one character and tries again, then another, walking backwards until the remainder of the pattern can match or every possibility is exhausted.
For "<b>.*</b>" on "<b>bold</b> and <b>bright</b>", the .* first swallows everything after the opening "<b>". The closing "</b>" then has nothing left to match, so the engine backtracks. The first position where "</b>" can match, walking from the end, is the final "</b>" in the string. Result: one match from the first tag to the last one, with bold</b> and <b>bright inside the .*. The pattern is not wrong; "everything between the first <b> and the last </b>" accurately describes what it found.
What lazy actually does
Adding ? directly after a quantifier flips its appetite. A lazy quantifier starts with the minimum: for .*? that is zero characters. The engine then tries the rest of the pattern immediately. Only when that fails does the quantifier take one more character and try again, growing step by step until the rest matches.
For "<b>.*?</b>" on the same string, the .*? starts empty, "</b>" does not match at that spot, so it grows by one character at a time until "</b>" lines up right after bold. The match ends at the first closing tag, not the last. Every quantifier has a lazy form: *?, +?, ?? and {n,m}?. The ? must sit immediately after the quantifier; position is what gives it this meaning.
The same pattern both ways, step by step
- Open the Regex Tester. Type
"<b>.*</b>"in the Pattern field, without slashes, and putgin the Flags field. Thegflag matters here: without it the tester reports only the first match, which hides the contrast you are about to create. - Paste the test text
"<b>bold</b> and <b>bright</b>". Read the match list: one match, index 0, spanning all 29 characters. - Change the pattern to
"<b>.*?</b>", leaving the text and the flag alone. The match list now shows two matches:"<b>bold</b>"at index 0 and"<b>bright</b>"at index 16, each with its own position. - Try the quotes version for a cleaner mental model. Text:
"one" and "two". Pattern".*"returns"one" and "two"as a single match. Pattern".*?"withgreturns"one"and"two".
That match list is the whole lesson. Greedy and lazy do not change what a pattern can match; they change which match, among several legal ones, the engine settles on, and where each match ends.
Worked example: two bold phrases
Suppose you are extracting labelled values from a log line that looks like "<b>bold</b> and <b>bright</b>", and you want each tagged phrase separately. The greedy run gives you one blob and a manual splitting job. The lazy run with g gives you exactly the list you wanted, each match with its index, ready to process.
Now flip the requirement. Suppose the string is start <b>bold</b> and <b>bright</b> end, and your job is to capture everything from the first tag to the last one as a single block. Now the greedy match is correct and the lazy one is the bug: it would stop at the first </b> and hand you a fragment. Neither quantifier is the good one. The string and the job decide, which is why reaching for .*? by reflex is as sloppy as never using it.
When greedy is the right choice
Choose greedy when the last delimiter is genuinely your endpoint: grabbing the final field of a line, matching up to the final closing bracket of a flat record, or taking the rest of a string after a marker, where .* at the end of a pattern is the normal idiom. Greedy also fits most single-value extraction, where the pattern is anchored and there is only one plausible match anyway.
When lazy is the right choice
Choose lazy when delimiters come in pairs and you want the contents of each pair: quoted strings, bracketed notes, simple tags, key-value spans in a log. The symptom is a match that starts at the right place and ends far too late, past one or more closing delimiters. If your result contains a closing delimiter followed by more text and then another closing delimiter, a greedy quantifier is the first suspect, and making it lazy is the first fix to try.
Limitations worth knowing
Lazy quantifiers do not make regex understand structure. "<b>.*?</b>" works on the flat test string because no <b> is nested inside another. Real HTML nests tags, and no quantifier setting can pair a tag with its correct closer when identical tags sit inside it; that needs a parser. Lazy matching can also cost more steps on long strings, because the engine extends the match character by character. And a lazy quantifier with a minimum of zero, like .*?, can match empty text where you did not intend it, which is why .+? is often safer when empty content is not valid.
Common mistakes
- Forgetting the
gflag. Without it, the tester reports only the first match, so a correct lazy pattern looks identical to a broken one. For any job about all the matches, setg. - Putting the
?in the wrong place..* ?and.*?are different patterns. The lazy marker must touch the quantifier it modifies. - Making everything lazy by habit. Lazy where greedy was correct gives you fragments instead of blocks. Decide from the job, not from superstition.
- Expecting lazy to fix nested tags. It cannot pair nested identical delimiters. Flat text only.
- Confusing the lazy
?with the optional?. Incolou?r, the?makes theuoptional. In.*?, the?makes the*lazy. Same character, different job, decided by what it follows.
FAQs
What does greedy mean in a regular expression?
A greedy quantifier consumes as much of the string as it can while still allowing the overall pattern to match. .* in the middle of a pattern runs to the end of the string first, then gives characters back one at a time until the rest of the pattern can match.
How do I make a quantifier lazy?
Put a question mark immediately after it. .* becomes .*?, .+ becomes .+?, and {2,5} becomes {2,5}?. The quantifier then starts with the fewest characters it is allowed and grows only while the rest of the pattern fails.
Is lazy always better than greedy? No. Lazy is right when you want the shortest match between paired delimiters, like quotes or tags. Greedy is right when you genuinely want everything up to the last delimiter, and it is also the cheaper default because the engine tries the longest run first instead of extending a short one step by step.
Why did my lazy pattern still return one long match?
Check the flag first: without the g flag the tester reports only the first match, which can hide the second short match you expected. Also check that the ? sits directly after the quantifier; a ? placed anywhere else means something different.
Can I use a lazy pattern to extract all the tags from real HTML?
For flat, simple markup in a test string, yes. For real HTML with nested tags, comments and attributes containing >, a regex is the wrong tool and an HTML parser is the honest answer.
Conclusion
Greedy takes the longest legal match, lazy takes the shortest, and the ? after a quantifier is the switch. Run both versions against a string with two pairs of delimiters in the Regex Tester and the match list makes the difference impossible to miss. Its close relative is the stacked-lookahead pattern, where the question is not how much text a quantifier takes but which ingredients a string must contain; that technique is covered in Regex for Password Strength. More guides live in the Developer Tools guides archive.