Regex Sets and Ranges

Summary: in this tutorial, you will learn about regex sets and ranges to create patterns that match characters in a set of characters.

Sets

When you place some characters inside square brackets [], you create a set. For example, the [aeiou] set matches any single lowercase vowel character.

Typically, a set can be used with regular characters. Also, a set may contain multiple characters, each character within a set corresponds to only one character in the match.

For example, licen[cs]e pattern matches both licence and license as shown in the following example:

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

var text = "A licence or license";
var pattern = @"licen[cs]e";

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

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

Output:

licence
licenseCode language: C# (cs)

Ranges

When a set has many characters e.g., from a to z, you don’t want to list them all in square brackets. Instead, you use a character range (-) like this:

[a-z]Code language: C# (cs)

The [a-z]1 is called a range that matches any character from a to z. Similarly, you can define a range of numbers from 0 to 9 as follows:

[0-9]Code language: C# (cs)

To define multiple ranges, you can place them next to each other inside the square brackets. For example:

[a-z0-9]Code language: C# (cs)

The range [a-z0-9] matches any character that is from a to z and 0 to 9.

The following example uses the regex ranges to define a pattern that matches the time string in the format hh:mm:

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


var text = "12:30 15:60";
var pattern = @"\b[0-2][0-9]:[0-5][0-9]\b";

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

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

Output:

12:30Code language: C# (cs)

The pattern

This pattern \b[0-2][0-9]:[0-5][0-9]\b matches the time format hh:mm, where the hour can be in the range of 00 to 23 and the minute can be in the range of 00 to 59.

Excluding sets and ranges

Regular expressions use the caret character ^ to negate a set or a range. For example, the range [^0-9] matches any character excluding a digit, which is equivalent to the \D character class.

It’s important to notice that when you use ^ inside a square bracket, it negates a set or a range. But if you use outside of a bracket, it is an anchor character that matches at the beginning of the string.

Summary

  • Use square brackets [] to define a set or a range.
  • Use the caret ^ inside the square bracket [] to negate a set or a range [^...].
Was this tutorial helpful ?