
Regex performance is important. It can be improved by using Regex as a field on classes. Another option is to use RegexOptions.Compiled. Avoiding static Regex calls also helps. There are many ways to optimize Regex calls.
Benchmark results Static Regex method: 6895 ms Instance Regex object: 6583 ms Instance compiled Regex: 5679 ms [fastest]

Here we look at the slowest of the examples in this document. This is the static Regex.Split method in System.Text.RegularExpressions. For the next three examples, I use Split, but other methods such as Matches, Match, and Replace have similar characteristics.
Program that uses Regex.Split [C#]
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string s = "This is a simple /string/ for Regex.";
string[] c = Regex.Split(s, @"\W+");
foreach (string m in c)
{
Console.WriteLine(m);
}
}
}
Output
This
is
a
simple
string
for
RegexDescription. This code uses the static Regex.Split method. Static methods are slower in cases like this where storing state would save CPU cycles. It demonstrates a simple Regex that Splits the input string into separate words. The \W+ means one or more non-word characters.
Here we see faster approach than the above example. This example creates an expression with new Regex. It works the same, but has better performance. It stores the Regex as a method-level instance.
Program that uses instance Regex [C#]
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string s = "This is a simple /string/ for Regex.";
Regex r = new Regex(@"\W+");
string[] c = r.Split(s);
foreach (string m in c)
{
Console.WriteLine(m);
}
}
}
Output
This
is
a
simple
string
for
RegexHere we see how you can use a compiled regular expression, and store it at the class level. We see two new approaches here. The Regex is stored as a static field, meaning it can be reused throughout the application without recreating it.
RegexOptions.CompiledProgram that uses static compiled Regex [C#]
using System;
using System.Text.RegularExpressions;
class Program
{
static Regex _wordRegex = new Regex(@"\W+", RegexOptions.Compiled);
static void Main()
{
string s = "This is a simple /string/ for Regex.";
string[] c = _wordRegex.Split(s);
foreach (string m in c)
{
Console.WriteLine(m);
}
}
}
Output
This
is
a
simple
string
for
Regex
Here we look at the performance characteristics of the regular expressions. The three Regex method calls above are compared here in one million iterations on the same method-level objects in the three examples. You can see the figures from the experiment at the top of this article.
Note: The benchmark code is not available.
Here we review some of the other work done by experts in the C# language and MSDN's resources. Microsoft's David Gutierrez states that there are three major options regarding regular expression performance. They are ordered from usually least efficient to most efficient.

First is interpreted regular expressions. The runtime parses the Regex into opcodes and then uses the interpreter. Creation time: low, runtime performance: low.
Second is compiled. Here you use RegexOptions.Compiled. Takes 10x longer to startup, but yields 30% better runtime. Don't use for dynamically generated Regexes. Creation time: highest, runtime performance: high.
Third is precompiled. You can use this as Regex.CompileToAssembly, but much harder to set up for new applications. Creation time: low, runtime performance: high.
BCL Team BlogMSDN. Next we look at MSDN which doesn't have extensive documentation here. It warns not to use RegexOptions.Compiled when also using CompileToAssembly. This means you can't combine compiled and precompiled code.
MSDN reference
Here we looked at one way you can optimize your Regex.Split regular expressions. We encountered a situation where runtime performance can be enhanced by sacrificing startup time. I feel that using an instance method that is not compiled is best for most situations, as it doesn't cost much during program startup.
Regex.Split Method Examples Regex Type