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);
}
}
Subscribe to:
Post Comments (Atom)
Archives
-
▼
2008
(167)
-
▼
September
(75)
- Use a Drag-and-Drop Operation
- Validate an Input Control
- Create an Animated System Tray Icon
- Make a Borderless Form Movable
- Immovable Forms Creation
- Making Multilingual Form in C#
- Using Part of a Main Menu for a Context Menu
- How To Link a Context Menu to a Control
- How to Sort a List View by Any Column
- How to Use an Autocomplete Combo Box
- Restrict a Text Box to Numeric Input
- Force a List Box to Scroll
- Save the Size and Location of a Form
- Find All MDI Child Forms
- Track the Visible Forms in an Application
- Process All the Controls on a Form
- Link Data to a Control in C#
- Add a Control Programmatically in C#
- Perform an XSL Transform
- Generate a Class from a Schema
- Create a Schema for a .NET Class
- Use XML Serialization with Custom Objects
- Validate an XML Document Against a Schema
- Read and Write XML Without Loading an Entire Docum...
- Find Elements with an XPath Search
- Get XML Nodes in a Specific XML Namespace
- Find Specific Elements by Name
- Quickly Append Nodes in an XML Document
- Insert Nodes in an XML Document
- Connecting to a Password-Protected Access Database...
- Connecting to a Microsoft Excel Workbook in ADO
- Connecting to an ODBC Data Source in ADO
- Ensure That Only One Instance of an Application Ca...
- Start a New Process
- Terminate a Process
- Create a Thread-Safe Collection Instance
- Synchronize the Execution of Multiple Threads
- How To Know When a Thread Finishes
- Control the Execution of a Thread
- Execute a Method Using a New Thread
- Execute a Method by Signaling a WaitHandle Object
- Execute a Method Using a Timer
- Execute a Method Asynchronously
- Execute a Method Using the Thread Pool
- Inspect the Attributes of a Program Element Using ...
- Create a Custom Attribute
- Instantiate an Object Using Reflection
- Test an Object's Type
- Retrieve Type Information
- Retrieve Type Information
- Unload Assemblies and Application Domains
- Pass Data Between Application Domains
- Instantiate a Type in a Different Application Domain
- Execute an Assembly in a Different Application Domain
- Load an Assembly into the Current Application Domain
- Create a Type That Can't Cross Application Domain ...
- Creating an Application Domain
- Connecting to an ODBC Data Source
- Avoid Loading Unnecessary Assemblies into Applicat...
- Pass Objects Across Application Domain Boundaries
- Store a Serializable Object to a File
- Create a Strongly Typed Collection
- Copy a Collection to an Array
- Sort an Array or an ArrayList
- Add, Subtract, and Compare Dates and Times
- Creating Dates and Times from Strings
- Use Compiled Regular Expressions
- Validate Input Using Regular Expressions in Csharp
- Encode Binary Data as Text in Csharp
- Convert Basic Value Types to Byte Arrays
- Encode a String Using Alternate Character Encoding
- Prevent People from Decompiling Your Code
- Manage the Global Assembly Cache in Csharp
- Create and Trust a Test Software Publisher Certifi...
- Sign an Assembly with an Authenticode Digital Sign...
-
▼
September
(75)
No comments:
Post a Comment