Language: EN

regex-cuantificadores

Quantifiers in Regex

Quantifiers allow us to specify the number of times a character, set of characters, or group must appear in the text string.

Instead of looking for an exact match, quantifiers add flexibility to the pattern by allowing variations in the number of repetitions.

There are several types of quantifiers in Regex, which are grouped into two main categories:

  1. Basic quantifiers: Specify an exact number of repetitions.
  2. Range quantifiers: Define a range of allowed repetitions.

Basic Quantifiers

First, let’s look at the basic quantifiers.

SymbolMatches
?Zero or one repetition of the preceding element
*Zero or more repetitions of the preceding element
+One or more repetitions of the preceding element

Zero or one time (?)

The quantifier ? indicates that the preceding character or group may appear zero or one time. It is useful for representing optional patterns.

ggle, gogle, google, gooogle, goooogle

Zero or more times (*)

The quantifier * indicates that the preceding element may appear zero or more times, which includes not appearing at all.

ggle, gogle, google, gooogle, goooogle

One or more times (+)

The quantifier + indicates that the character or group must appear at least once, but may repeat indefinitely.

ggle, gogle, google, gooogle, goooogle

Range Quantifiers

In addition to basic quantifiers, Regex allows you to define quantifiers with exact limits or ranges of repetitions.

SymbolMatches
{n}Exactly n repetitions of the preceding element
{n,}At least n repetitions of the preceding element
{n,m}Between n and m repetitions of the preceding element

Exactly n times ({n})

The quantifier {n} specifies that the character or group must appear exactly n times.

ggle, gogle, google, gooogle, goooogle

At least n times ({n,})

The quantifier {n,} indicates that the character or group must appear at least n times, with no upper limit.

ggle, gogle, google, gooogle, goooogle

Between n and m times ({n,m})

The quantifier {n,m} allows you to set a range of repetitions for the character or group, where the pattern must appear at least n times and at most m times.

ggle, gogle, google, gooogle, goooogle