Regular Expression - Greedy Quantifier

Regexp

About

Greedy quantifier

A Greedy quantifier will match the longest possible string (ie they consume as much input as possible) whereas Lazy quantifier will match the shortest possible string. Match as few as possible, repeat as few times as possible.

List

Quantifier
* Matches 0 or more occurrences
of the preceding element. See Regexp - Star
ab*c matches ac, abc, abbc, abbbc, and so on
+ Matches 1 or more occurrences
of the preceding element
ab+c matches abc, abbc, abbbc, and so on, but not ac
? Matches 0 or 1 occurrence
of the preceding element
colou?r matches both color and colour
{ start min/max quantifier
} end min/max quantifier
{m} Matches exactly m times
{m,} Matches at least m times
{m,n} Matches at least m times but no more than n times

Example

Hello World

For example in the termhello

  • the greedy h.+l matches hell
  • but the lazy h.+?l matches hel.

HTML

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

  • The lazy pattern <.+?> will match and





Discover More
Card Puncher Data Processing
Antlr - Lexer Rule (Token names|Lexical Rule)

in Antlr. They are rules that defines tokens. They are written generally in the grammar but may be written in a lexer grammar file Each lexer rule is either matched or not so every lexer rule expression...
Compiler
Grammar - Rule Construct / Basic Rule Expression

This page lists common rule expression in order to describe the structure of the parsed document. A rule expression is a pattern expression that produces a boolean and are therefore predicate. grammar...
Regexp
Regexp - (Quantifier|Cardinality Indicators)

A quantifier defines the number of times that: character class of character grouped (or not) may be seen. It has three behaviors: a Greedy one: match longest possible string. This is the...
Regexp
Regular Expression - (Lazy|Reluctant) Quantifier

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. To make a match...
Regexp
Regular Expression - Possessive Quantifier

You add a + to the greedy qualifier to make it possessive. 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,}+...



Share this page:
Follow us:
Task Runner