Over a million developers have joined DZone.

How to run regular expressions faster in C#

· Performance Zone

Download Forrester’s “Vendor Landscape, Application Performance Management” report that examines the evolving role of APM as a key driver of customer satisfaction and business success, brought to you in partnership with BMC.

If you want the regular expression to run faster , you can set the RegexOptions.Compiled value to the regex constructor. This will compile the regex in to their own assembly which will run a little faster.

Below is a sample code snippet demonstrating the usage of RegexOptions.Compiled.

//  Validating a Zip codes in C# using Regular Expression
2
string GKZipCode = @"^\d{5}(-?\d{4})?$";
3
Regex regex = new Regex(GKZipCode, RegexOptions.Compiled);
4
bool Matching = regex.IsMatch("15689");
5
Console.WriteLine(Matching);
6
Console.ReadLine();


See Forrester’s Report, “Vendor Landscape, Application Performance Management” to identify the right vendor to help IT deliver better service at a lower cost, brought to you in partnership with BMC.

Topics:

Published at DZone with permission of Senthil Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}