TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Static Regex

This C# article shows a static Regex. It describes the benefits of static Regex instances.

Static Regex. 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.

Example. To start, we demonstrate the use of the static regular expression object. 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.

Regex.Matches

Next: This program finds the first word starting with a capital R in the input string.

C# program that uses static Regex instance

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

The biggest benefit of static Regex instances is that they will not likely be created more than once. Your regular expression can be shared between many different methods on the type. This will improve performance.

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.

Regex Performance

Also: Static fields do not have an instance expression, so resolving the location of their memory storage is slightly faster.

Summary. We implemented a static regular expression. We further 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.

Review: Regular expressions are not typically the ideal solution for performance concerns.

But: They can be greatly enhanced with careful considerations of their allocation patterns, as with the static modifier.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf