Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to run regular expressions faster in C#

DZone's Guide to

How to run regular expressions faster in C#

· Performance Zone ·
Free Resource

SignalFx is the only real-time cloud monitoring platform for infrastructure, microservices, and applications. The platform collects metrics and traces across every component in your cloud environment, replacing traditional point tools with a single integrated solution that works across the stack.

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();


SignalFx is built on a massively scalable streaming architecture that applies advanced predictive analytics for real-time problem detection. With its NoSample™ distributed tracing capabilities, SignalFx reliably monitors all transactions across microservices, accurately identifying all anomalies. And through data-science-powered directed troubleshooting SignalFx guides the operator to find the root cause of issues in seconds.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}