Abstract [ Documentation]

The .NET regular expression engine is a backtracking regular expression matcher. It employs a nondeterministic finite automation (NFA) engine like that used in Perl, Python, and other languages.

Regex Engine Type
[ more information]
ExamplesProcessing
driven by…
Guaranteed to
find longest
match possible
May test same
character twice
Supports
backtracking
Can match
backreferences
Can capture
sub-expressions
Deterministic
Finite
Automation
.NET, Perl, Python, TclInput stringYesNo (faster)NoNoNo
Traditional
Nondeterministic
Finite
Automation
awk, egrep, lexRegular expression patternNoYes (slower)YesYesYes
POSIX
Nondeterministic
Finite
Automation
N/ARegular expression patternYesYes (slowest)YesYesYes

In .NET, the regular expression engine needs a minimum of two pieces of information:

  1. The regular expression pattern to identify the text.
  2. The text to parse for the regular expression.

.NET’s regex implementation is compatible with Perl 5.

Create a Regex

public class Foo 
{
    public Regex regex = new Regex(@"abc|def", RegexOptions.IgnoreCase);

    public bool Bar(string input) 
    {
        bool isMatch = regex.IsMatch(input);
        // ...
    }
}

System.Text.RegularExpressions

The Regex type in this class represents the .NET regular expression engine. It can perform the following operations:

  • Regex.IsMatch(input, pattern) — determine if the regular expression pattern occurs in the text.
  • Regex.Match(input, pattern) — retrieve one occurrence of a match and return it in a RegularExpressions.Match object.
  • Regex.Matches(input, pattern) — retrieve all occurrences of a match and return them in a RegularExpressions.MatchCollection object.
  • Note: MatchCollection is empty if no matches were found.
  • Note: By default, this method uses lazy evaluation to populate the MatchCollection. Use foreach to traverse the collection.
  • Regex.Replace(input, pattern) — replace text that matches the regular expression pattern

Match

The result of a single regex match.

  • Match.Success — bool if a match was found
  • Match.NextMatch() — return the next substring of input that matches pattern
  • Match.Groups — a GroupCollection object that contains information about the substrings that match capturing groups used in the regex pattern
  • Match.Value — a substring in input that matches pattern
  • Match.Index — the zero-based index of the matched substring in input

MatchCollection

A collection of Match objects representing all the matches found in the order in which they occur in the input string.

  • This object is empty if no matches are found.

GroupCollection

A collection of Group objects that represent captured groups from a single match:

  • Group.Value — the captured substring
  • Group.Index — the starting position of the captured group in the input text
  • Group.Length — the length of the captured substring
  • Group.Success — bool if a substring matched the pattern defined by the capturing group

GroupCollection[0] is the entire match. Each following element is the result of a single capture group.

Unnamed groups can be retrieved via their index.

  • They appear first in the collection.
  • Calling Regex.GetGroupNumbers() returns what numbered groups are available.

Named groups can be retrieved via their name or index.

  • Calling Regex.GetGroupNames() returns what named groups are available.

Group

The result from a single capturing group.

Miscellaneous