What is the difference between greedy and lazy regex matching?
Greedy and lazy quantifiers control how much text a regex pattern consumes.
Greedy matching (default):
Quantifiers like *, +, and {n,m} are greedy by default:*
- They match as much text as possible
<.*>matching<div>text</div>captures the entire string, not individual tags- The pattern expands as far as it can while still allowing the overall match to succeed*
Lazy (non-greedy) matching:
Add ? after a quantifier to make it lazy:
*?,+?,{n,m}?match as little as possible<.*?>matching<div>text</div>captures<div>first, then</div>separately- The pattern stops as soon as the condition is satisfied
Practical example:
Extracting text between HTML tags:
- Greedy:
<div>(.*)</div>on<div>A</div><div>B</div>matchesA</div><div>B - Lazy:
<div>(.*?)</div>correctly matchesAfirst, thenBseparately
Performance considerations:
- Greedy matching can be faster in some regex engines
- Lazy matching prevents common errors when working with markup
- Choose based on your specific matching needs, not just performance
Common pitfall:
The most common regex mistake is using greedy quantifiers when extracting content between delimiters. Always consider if lazy matching would be more appropriate for your use case.