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] | Examples | Processing 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, Tcl | Input string | Yes | No (faster) | No | No | No |
| Traditional Nondeterministic Finite Automation | awk, egrep, lex | Regular expression pattern | No | Yes (slower) | Yes | Yes | Yes |
| POSIX Nondeterministic Finite Automation | N/A | Regular expression pattern | Yes | Yes (slowest) | Yes | Yes | Yes |
In .NET, the regular expression engine needs a minimum of two pieces of information:
- The regular expression pattern to identify the text.
- 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 aRegularExpressions.Matchobject.Regex.Matches(input, pattern)— retrieve all occurrences of a match and return them in aRegularExpressions.MatchCollectionobject.- Note:
MatchCollectionis empty if no matches were found. - Note: By default, this method uses lazy evaluation to populate the
MatchCollection. Useforeachto 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 foundMatch.NextMatch()— return the next substring of input that matches patternMatch.Groups— aGroupCollectionobject that contains information about the substrings that match capturing groups used in the regex patternMatch.Value— a substring in input that matches patternMatch.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 substringGroup.Index— the starting position of the captured group in the input textGroup.Length— the length of the captured substringGroup.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
System.Web.RegularExpressionscontains RegEx options that implement predefined RegEx patterns for parsing HTML, XML, and other structured documents.System.Configuration.RegexStringValidator— validate a string against the rules of a regular expression.- Regular expression pattern examples: