
A static Regex helps performance. It can be used throughout methods in the C# program. It is essentially a single-instance regular expression. Static regular expressions have clear performance advantages.
This C# article shows a static Regex. It describes the benefits of static Regex instances.

To start, we demonstrate the use of the static regular expression object in the .NET Framework and C# programming language. You can use a static variable initializer at the class level to instantiate the regular expression with the new operator. Then, you can access the Regex by its identifier and call methods such as Match, Matches, and IsMatch on it. This program finds the first word starting with a capital R in the input string.
Program that uses static Regex instance [C#]
using System;
using System.Text.RegularExpressions;
class Program
{
static Regex _rWord = new Regex(@"R\w*");
static void Main()
{
// Use the input string.
// ... Then try to match the first word starting with capital R.
string value = "This is a simple /string/ for Regex.";
Match match = _rWord.Match(value);
Console.WriteLine(match.Value);
}
}
Output
Regex
Benefits of static Regex instances. The biggest benefit of static Regex instances is that they will not likely be created and allocated more than once. Your regular expression can be shared between many different methods on the type. This will improve performance, sometimes significantly depending on the program's utility.

Performance benchmarks. Static regular expressions show a performance advantage in timing tests. For more details about this performance differential, please see the regular expression performance article on this web site. Also, static fields do not have an instance expression, so resolving the location of their memory storage is slightly faster.
Regex Performance
We saw how you can implement a static regular expression in the C# programming language. Further, we noted how this can improve performance by enhancing the access patterns to the regular expression and restricting the allocations of regular expressions on the heap. Regular expressions, while not typically ideal for performance concerns, can be greatly enhanced with careful considerations of their allocation patterns, as with the static modifier.
Static Modifier