Skip to main content

Character Classes and Quantifiers

Quantifiers: Controlling Repetition

0:00
LearnStep 1/3

Basic Quantifiers

Understanding Basic Quantifiers

Quantifiers are fundamental to Regular Expressions because they allow you to define how many times a specific character, group, or character class should appear in a match. Instead of writing \d\d\d to match three digits, you can use quantifiers to be more expressive and flexible.

Standard Quantifiers

  • * (Asterisk): Matches zero or more repetitions of the preceding element. It is equivalent to {0,}.
  • + (Plus): Matches one or more repetitions. It ensures the element exists at least once. Equivalent to {1,}.
  • ? (Question Mark): Matches zero or one repetition. This effectively makes the preceding element optional. Equivalent to {0,1}.

Precise Repetition with Curly Braces

For more control, you can specify exact counts or ranges using curly braces:

  • {n}: Matches exactly n times.
  • {n,}: Matches n or more times.
  • {n,m}: Matches between n and m times (inclusive).

Practical Python Examples

python