Skip to content

Regex in Dotnet

Regular expressions can be a useful tool to validate, replace and extract information from text. For a general explanation of regular expressions see Regular Expressions.

To use regex in dotnet you can use the Regex class from the System.Text.RegularExpressions namespace. Here are some common use cases:

Validate if a pattern matches a text

// Does text consist of exactly 3 digits?
var text = "147";
bool matches = Regex.IsMatch(text, @"^\d{3}$");
// matches: true

Note: It is advisable to define the pattern as a verbatim string (using the @-Symbol before the string). Escape sequences such as \n won’t be interpreted as escape sequences, but instead as literal characters.

Extract information from text

// Extract all kg values from a text.
var text = "2kg + 3kg = 5kg";
var matches = Regex.Matches(text, @"(\d+)kg");
var results = new List<int>();
foreach (Match m in matches)
{
results.Add(int.Parse(m.Value));
}
// results: [2, 3, 5]

Replace text

// Replace all good grades with very good
var text = "Student1: good, Student2: very good";
text = Regex.Replace(text, @"(?<!very\s)good");
// text: "Student1: very good, Student2: very good"

Optimizing Regular Expressions When making excessive use of regular expressions you might want to try optimizing their execution. Using a compiled regex, instead of evaluating the regular expression algorithmically, IL code will be generated that is transformed to machine code by the JIT compiler which can speed up the execution drastically. The initial creation of the regular expression will be slower, but when reused often, it can pay off.

// compile a regular expression into an object
var containsNumberRegex = new Regex(@"\d+", RegexOptions.Compiled);
// use that object for matching/extracting/replacing
containsNumberRegex.IsMatch("abc123");