Regex Quantifiers

Summary: in this tutorial, you will learn about regex quantifiers that specify the quantity of the preceding element in a regular expression pattern.

Introduction to the Regex Quantifiers

The digit character class \d matches any digit e.g., 0 to 9. To match two consecutive digits, you need to repeat the digit character class like \d\d. Similarly, to match a group of four digits, you use four digit character classes \d\d\d\d.

If you want to match one or more digits, then this solution does not work because you don’t know the number of digits that you want to repeat. That’s why the quantifiers come to the rescue.

Quantifiers are metacharacters in regular expressions that specify the quantity of the preceding element. They define a specified number of times a particular element should occur in an input string when matching.

For example, the quantifier + matches the occurrences of the preceding element one or more times. Therefore, the \d+ matches one or more digit characters.

The following table lists all the quantifiers and their meanings:

QuantifierNameMeaning
*AsteriskMatch the occurrences of the preceding element zero or more times.
+PlusMatch the occurrences of the preceding element one or more times.
?Question MarkMatch the occurrences of the preceding element zero or one time.
n }Curly BracesMatch the occurrences of the preceding element exactly n times.
n ,}Curly BracesMatch the occurrences of the preceding element at least n times.
n , m }Curly BracesMatch the occurrences of the preceding element from n to m times.

Regex Quantifier examples

Let’s take some examples of using the regex quantifiers.

1) Matching the occurrences of the preceding element zero or more times (*)

The following example uses the quantifier (*) to match a text that starts with zero or more characters and ends with the letters “at“:

using System.Text.RegularExpressions;
using static System.Console;


var text = "The cat sat on the mat at the park.";
var pattern = @"\w*at";

var matches = Regex.Matches(text, pattern);

foreach (var match in matches)
{
    WriteLine(match);
}Code language: C# (cs)

Output:

cat
sat
mat
atCode language: C# (cs)

The following explains the meaning of the regular expression pattern \w*at :

  • \w matches any word character.
  • * is a quantifier that matches zero or more occurrences of the preceding element, which in this case is \w.
  • at matches the literal character “at“.

So the pattern \w*at will match any sequence of word characters followed by the letters “at“.

2) Matching the occurrences of the preceding element zero or more times (+)

The following example uses the quantifier (+) with \d to match one or more consecutive digits in a string:

using System.Text.RegularExpressions;
using static System.Console;


var text = "C# 12 and .NET 8";
var pattern = @"\d+";

var matches = Regex.Matches(text, pattern);

foreach (var match in matches)
{
    WriteLine(match);
}Code language: C# (cs)

Output:

12
8Code language: C# (cs)

The pattern \d+:

  • \d matches any digit.
  • + is a quantifier that matches one or more occurrences of the preceding element, which is \d in this case.

So \d+ matches a sequence of digits.

3) Matching the occurrences of the preceding element one or more times (?)

The following example shows how to use the quantifier (*?) to match both words color and colour:

using System.Text.RegularExpressions;
using static System.Console;


var text = "color or colour";
var pattern = @"colou?r";

var matches = Regex.Matches(text, pattern);

foreach (var match in matches)
{
    WriteLine(match);
}Code language: C# (cs)

Output:

color
colourCode language: C# (cs)

4) Matching the occurrences of the preceding element exactly n times {n}

The following example uses the quantifier {n} with the \d to match exactly two digits:

using System.Text.RegularExpressions;
using static System.Console;


var text = "It's 12:30 December 1, 2000";
var pattern = @"\d{2}";

var matches = Regex.Matches(text, pattern);

foreach (var match in matches)
{
    WriteLine(match);
}Code language: C# (cs)

Output:

12
30
20
00Code language: C# (cs)

In this example, the regular expression pattern \d{2} matches exactly two consecutive digits. Therefore, it returns 12, 30, 20, and 00.

5) Match the occurrences of the preceding element at least n times {n, }

The following example uses the {n,} quantifier to match the time in both hh:mm and h:mm formats:

using System.Text.RegularExpressions;
using static System.Console;


var text = "06:20 6:30";
var pattern = @"\d{1,}:\d{1,}";

var matches = Regex.Matches(text, pattern);

foreach (var match in matches)
{
    WriteLine(match);
}Code language: C# (cs)

Output:

06:20
6:30Code language: C# (cs)

6) Match the occurrences of the preceding element from n to m times {n, m}

The following example uses the {n,m} quantifier to match a date in the format m-d-yyyy or mm-dd-yyyy:

using System.Text.RegularExpressions;
using static System.Console;


var text = "12-31-1999 to 1-1-2000";
var pattern = @"\d{1,2}-\d{1,2}-\d{4}";

var matches = Regex.Matches(text, pattern);

foreach (var match in matches)
{
    WriteLine(match);
}Code language: C# (cs)

Output:

12-31-1999
1-1-2000Code language: C# (cs)

Summary

  • Use regex quantifiers to match a preceding pattern a specified number of times.
Was this tutorial helpful ?