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. Static regular expressions show a performance advantage in timing tests. Also, static fields do not have an instance expression, so resolving the location of their memory storage is slightly faster.
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);
}
}
Out Put:
Regex