Regular Expressions APIs in C#

Learn string matching with a given pattern using regular expressions, and use quantifiers and boundaries to efficiently define these patterns.

Regular expressions API

The .NET framework provides a rich and comprehensive Regex class, defined within the System.Text.RegularExpressions namespace, to match patterns of text via regular expressions.

We can use the following using statement to access the regular expressions API in our program:

using System.Text.RegularExpressions;

This System.Text.RegularExpressions namespace contains the following useful classes and interfaces.

Useful Classes and Interfaces

Class or Interface

Description

Regex



It represents a compiled regular expression and performs various operations like pattern matching and replacing patterns on strings.

Match

It represents the results of a single regular expression match.

MatchCollection

It represents the results of multiple regular expression matches.

Capture

It represents a captured group within a Match object.

CaptureCollection

It represents a collection of Capture objects.

Group

It represents a capturing group within a Match object.

GroupCollection

It represents a collection of Group objects.

RegexOptions

It enumerates the options that can be set when compiling a regular expression.

This section focuses on implementing regular expressions for various use cases using the Regex class in .NET.

Note: We use C# 3+, notably dynamic typing, to take full advantage of C#'s language features.

mportant classes in the System.Texts.RegularExpressions namespace

The Regex class

Description

...

Get hands-on with 1400+ tech skills courses.