Subscribe

RSS Feed (xml)

Use Compiled Regular Expressions

By default, when you create a Regex object, the regular expression pattern you specify in the constructor is compiled to an intermediate form (not MSIL). Each time you use the Regex object, the runtime interprets the pattern's intermediate form and applies it to the target string. With complex regular expressions that are used frequently, this repeated interpretation process can have a detrimental effect on the performance of your application. By specifying the RegexOptions.Compiled option when you create a Regex object, you force the .NET runtime to compile the regular expression to MSIL, instead of the interpreted intermediary form. This MSIL is just-in-time (JIT) compiled by the runtime to native machine code on first execution—just like regular assembly code. You use a compiled regular expression in the same way as you use any Regex object; compilation simply results in faster execution. However, there are downsides to offset the performance benefits provided by compiling regular expressions. First, the JIT compiler has to do more work, which will introduce delays during JIT compilation. This is most noticeable if you create your compiled regular expressions as your application starts up. Second, the runtime can't unload a compiled regular expression once you have finished with it. Unlike a normal regular expression, the runtime's garbage collector won't reclaim the memory used by the compiled regular expression. The compiled regular expression will remain in memory until your program terminates or you unload the application domain in which the compiled regular expression is loaded. This code shows how to create a Regex object that's compiled to MSIL instead of the usual intermediate form. Regex reg = new Regex(@"[\w-]+@([\w-]+\.)+[\w-]+", RegexOptions.Compiled); In addition, the static Regex.CompileToAssembly method allows you to create a compiled regular expression and write it to an external assembly. This means you can create assemblies containing standard sets of regular expressions that you can use from multiple applications. To compile a regular expression and persist it to an assembly, take the following steps: Create a System.Text.RegularExpressions.RegexCompilationInfo array large enough to hold one RegexCompilationInfo object for each of the compiled regular expressions you want to create. Create a RegexCompilationInfo object for each of the compiled regular expressions, and specify values for its properties as arguments to the object constructor; the most commonly used properties are IsPublic, a bool that specifies whether the generated regular expression class has public visibility. Name, a String that specifies the class name. Namespace, a String that specifies the namespace of the class. Pattern, a String that specifies the pattern that the regular expression will match. (See recipe 2.5 for more details.) Options, a System.Text.RegularExpressions.RegexOptions value that specifies options for the regular expression. Create a System.Reflection.AssemblyName object, and configure it to represent the name of the assembly that the Regex.CompileToAssembly method will create. Execute Regex.CompileToAssembly, passing the RegexCompilationInfo array and the AssemblyName object. This process creates an assembly that contains one class declaration for each compiled regular expression—each class derives from Regex. To use the compiled regular expression contained in the assembly, instantiate the regular expression you want to use and call its method as if you had simply created it with the normal Regex constructor. (Remember to add a reference to the assembly when you compile the code that uses the compiled regular expression classes.) The following code shows how to create an assembly named MyRegEx.dll, which contains two regular expressions named PinRegex and CreditCardRegex. using System.Text.RegularExpressions; using System.Reflection; public class CompiledRegexExample { public static void Main() { // Create the array to hold the Regex info objects RegexCompilationInfo[] regexInfo = new RegexCompilationInfo[2]; // Create the RegexCompilationInfo for PinRegex regexInfo[0] = new RegexCompilationInfo(@"^\d{4}$", RegexOptions.Compiled, "PinRegex", "", true); // Create the RegexCompilationInfo for CreditCardRegex regexInfo[1] = new RegexCompilationInfo( @"^\d{4}-?\d{4}-?\d{4}-?\d{4}$", RegexOptions.Compiled, "CreditCardRegex", "", true); // Create the AssemblyName to define the target assembly AssemblyName assembly = new AssemblyName(); assembly.Name = "MyRegEx"; // Create the compiled regular expression Regex.CompileToAssembly(regexInfo, assembly); } }

No comments:

Post a Comment

Archives

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail