What do 'lazy' and 'greedy' mean in the context of regular expressions?
In regular expressions, the terms lazy (also called non-greedy) and greedy describe how quantifiers match text:
-
Greedy Quantifiers (e.g.
*,+,?,{m,n}without a?)- Match as much as possible before allowing the rest of the pattern to proceed.
- Examples:
.*tries to consume as many characters as it can while still leaving the pattern able to match..+requires at least one character, then again tries to take as many as possible.
-
Lazy Quantifiers (also called non-greedy, formed by adding
?after a greedy symbol, e.g.*?,+?,??,{m,n}?)- Match as few characters as possible before allowing the rest of the pattern to proceed.
- Examples:
.*?tries to consume as few characters as it can while still allowing the pattern to match..+?must match at least one character, but then it stops as soon as the next part of the regex can match.
How It Affects Matching
- Greedy: Captures the longest valid string that fits the pattern.
- Lazy: Captures the shortest valid string that fits the pattern.
Example
If you have the string:
abcENDdefENDghi
and the pattern:
^(.*)END
- The greedy
.*will match as much as possible while still leavingENDto match at the end. - Thus,
(.*)might matchabcENDdefso thatENDat the second occurrence satisfies the pattern.
In contrast, if the pattern is:
^(.*?)END
- The lazy
.*?will match as few characters as it can beforeEND. - So
(.*?)will captureabc(stopping at the firstEND) rather than skipping over the firstEND.
When to Use Each
-
Greedy (
.*):- Default behavior.
- Useful when you want to gather as much text as possible until the next part of the pattern can match.
-
Lazy (
.*?):- Use when you only need the first or smallest match.
- Particularly helpful if you have a delimiter or terminator string (e.g., “Match everything until the first
END”).
Key Points
- All standard quantifiers (
*,+,?,{m,n}) are greedy by default. - You make them lazy or non-greedy by adding a
?after them (e.g.,*?,+?). - Greedy means “grab as much as possible,” while lazy means “grab as little as possible” while still letting the rest of the pattern match.
Further Learning: Regex & JavaScript
If you want to improve at regex and JavaScript for real-world coding or interview scenarios, here are some recommended resources from DesignGurus.io:
-
Grokking JavaScript Fundamentals
Dive deeper into closures, prototypes, async/await, and more—vital for advanced regex usage and debugging in JS. -
Grokking the Coding Interview: Patterns for Coding Questions
Strengthen your problem-solving approach with proven coding patterns used in technical interviews.
For personalized feedback, try:
And explore the DesignGurus.io YouTube channel for free tutorials on system design, coding patterns, and more.
Summary: “Greedy” quantifiers match as much text as possible, while “lazy” (or non-greedy) quantifiers match as little as possible, enabling more fine-grained control over how the pattern consumes characters.