Logo

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:

  1. 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.
  2. 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 leaving END to match at the end.
  • Thus, (.*) might match abcENDdef so that END at the second occurrence satisfies the pattern.

In contrast, if the pattern is:

^(.*?)END
  • The lazy .*? will match as few characters as it can before END.
  • So (.*?) will capture abc (stopping at the first END) rather than skipping over the first END.

When to Use Each

  1. Greedy (.*):

    • Default behavior.
    • Useful when you want to gather as much text as possible until the next part of the pattern can match.
  2. 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:

  1. Grokking JavaScript Fundamentals
    Dive deeper into closures, prototypes, async/await, and more—vital for advanced regex usage and debugging in JS.

  2. 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.

CONTRIBUTOR
TechGrind