Regular Expression - (Lazy|Reluctant) Quantifier

> Procedural Languages > Multilingual Regular Expression Syntax (Pattern)

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.

Advertising

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

5.2 - HTML

<em>Hello World</em>
  • The greedy pattern <.+> will match <em>Hello World</em>
  • The lazy pattern <.+?> will match <em> and </em>

6 - Documentation / Reference

lang/regexp/lazy.txt · Last modified: 2019/06/13 11:50 by gerardnico