Regular expressions are a powerful tool in the programming world, allowing developers to efficiently search, parse, and manipulate text. However, they can also be a source of frustration, as the syntax and patterns can be complex and difficult to remember. In this article, we will explore the various ways to use regular expressions and provide practical examples to help demystify this topic. Additionally, we will share tips for optimizing regular expression performance based on the latest best practices. If you want to improve your skills in this area, check out my book "Rock Your Code: Code & App Performance for Microsoft.NET" available on Amazon.com.
If you’ve never used a regular expression, this is a description:
A regular expression, often abbreviated as "regex" or "regexp", is a sequence of characters that define a search pattern. Regular expressions are used to search for and match patterns in text and manipulate text based on those patterns. They can be used in a variety of programming languages, tools, and applications to perform tasks such as validation, data extraction, and text manipulation. Regular expressions are made up of a combination of characters, including literal characters, metacharacters, and quantifiers, which define the rules for the pattern to be matched.
The concept of regular expressions can be traced back to the 1940s when mathematician Stephen Kleene introduced the concept of regular sets and regular languages. In the 1960s, Ken Thompson, a computer scientist at Bell Labs, developed the first implementation of regular expressions as part of the QED text editor. The regular expression syntax was later standardized and popularized in the Unix world by tools such as grep, sed, and awk.
In the Beginning
Using regular expressions has been in .NET ever since the first version. The first part of a regular expression is to come up with the pattern that will be used for matching or replacing strings. For example, this is the pattern that I used to ensure a string contains a word.
\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
This is how we use that pattern in code.
public static bool ContainsWord(string input){
var expression = new Regex(
pattern: @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
options: RegexOptions.CultureInvariant);
return expression.IsMatch(input);
}
Then Came a Better Way
Regular expressions are fast but recently another way came along in .NET to use them to increase performance. The “magic” is that now we use them from a field in a class. First, we move the call to a field like this.
private static readonly Regex _containsWordRegEx =
new(pattern: @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
options: RegexOptions.CultureInvariant);
Then we use the field instead of creating a new Regex object whenever needed.
public static bool ContainsWord(string input) =>
_containsWordRegEx.IsMatch(input);
As my code performance book described, this dramatically increases the performance (see benchmark results below).





Sahriar SharifPosted Apr 16, 2023, 11:25 AM
Nicely explained. Thanks