O'Reilly logo

C# Cookbook by Jay Hilyard, Stephen Teilhet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.8. Compiling Regular Expressions

Problem

You have a handful of regular expressions to execute as quickly as possible over many different strings. Performance is of the utmost importance.

Solution

The best way to do this task is to use compiled regular expressions. However, there are some drawbacks to using this technique, which we will examine.

There are two ways to compile regular expressions. The easiest way is to use the RegexOptions.Compiled enumeration value in the Options parameter of the static Match or Matches methods on the Regex class:

Match theMatch = Regex.Match(source, pattern, RegexOptions.Compiled);

MatchCollection theMatches = Regex.Matches(source, pattern, RegexOptions.Compiled);

If more than a few expressions will be compiled and/or the expressions need to be shared across applications, consider precompiling all of these expressions into their own assembly. Do this by using the static CompileToAssembly method on the Regex class. The following method accepts an assembly name and compiles two simple regular expressions into this assembly:

public static void CreateRegExDLL(string assmName) { RegexCompilationInfo[] RE = new RegexCompilationInfo[2] {new RegexCompilationInfo("PATTERN", RegexOptions.Compiled, "CompiledPATTERN", "Chapter_Code", true), new RegexCompilationInfo("NAME", RegexOptions.Compiled, "CompiledNAME", "Chapter_Code", true)}; System.Reflection.AssemblyName aName = new System.Reflection.AssemblyName( ); aName.Name = assmName; Regex.CompileToAssembly(RE, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required