Regular Expression - (Lazy|Reluctant) Quantifier
Table of Contents
1 - About
A Lazy quantifier will match the shortest possible string. Match as few as possible, repeat as few times as possible whereas a Greedy quantifier will match the longest possible string.
2 - Articles Related
3 - Syntax
To make a match lazy, you add a question mark after the greedy quantifier.
4 - List
Lazy Quantifier | Description |
---|---|
X?? | X, once or not at all |
X*? | X, zero or more times |
X+? | X, one or more times |
X{n}? | X, exactly n times |
X{n,}? | X, at least n times |
X{n,m}? | X, at least n but not more than m times |
5 - Example
5.1 - Hello World
For example in the termhello
- the greedy h.+l matches hell
- but the lazy h.+?l matches hel.
5.2 - HTML
<em>Hello World</em>
- The greedy pattern <.+> will match <em>Hello World</em>
- The lazy pattern <.+?> will match <em> and </em>