- Introduction
- Creating and Using String Objects
- Formatting Strings
- Accessing Individual String Characters
- Analyzing Character Attributes
- Case-Insensitive String Comparison
- Working with Substrings
- Using Verbatim String Syntax
- Choosing Between Constant and Mutable Strings
- Optimizing StringBuilder Performance
- Understanding Basic Regular Expression Syntax
- Validating User Input with Regular Expressions
- Replacing Substrings Using Regular Expressions
- Building a Regular Expression Library
3.13. Building a Regular Expression Library
You want to create a library of regular expressions that you can reuse in other projects.
Technique
Use the CompileToAssembly static method within the Regex class to compile a regular expression into an assembly. This method uses an array of RegexCompilationInfo objects that contain any number of regular expressions you want to add to the assembly.
The RegexCompilationInfo class contains a constructor with five fields that you must fill out. The parameters denote the string for the regular expression; any options for the regular expression, which appear in the RegexOptions enumerated type; a name for the class that is created to hold the regular expression; a corresponding namespace; and a Boolean value specifying whether the created class should have a public access modifier.
After creating the RegexCompilationInfo object, create an AssemblyName object, making sure to reference the System.Reflection namespace, and set the Name property to a name you want the resultant assembly filename to be. Because the CompileToAssembly creates a DLL, exclude the DLL extension on the assembly name. Finally, place all the RegexCompilationInfo objects within an array, as shown in Listing 3.12, and call the CompileToAssembly method. Listing 3.12 demonstrates how to create a RegexCompilationInfo object and how to use that object to compile a regular expression into an assembly using the CompileToAssembly method.
Listing 3.12 Using the CompileToAssembly Regex Method to Save Regular Expressions in a New Assembly for Later Reuse
using System; using System.Text.RegularExpressions; using System.Reflection; namespace _12_RegExpReplace { class Class1 { [STAThread] static void Main(string[] args) { CompileRegex(@"(\d{4})-(\d{4})-(\d{4})-(\d{4})", @"regexlib" ); } static void CompileRegex( string exp, string assemblyName ) { RegexCompilationInfo compInfo = new RegexCompilationInfo( exp, 0, "CreditCardExp", "", true ); AssemblyName assembly = new AssemblyName(); assembly.Name = assemblyName; RegexCompilationInfo[] rciArray = { compInfo }; Regex.CompileToAssembly( rciArray, assembly ); } } }
Comments
If you use regular expressions regularly, then you might find it advantageous to create a reusable library of the expressions you tend to use the most. The Regex class contains a method named CompileToAssembly that allows you to compile several regular expressions into an assembly that you can then reference within other projects.
Internally, you will find a class for each regular expression you added, all contained within its corresponding namespace, as specified in the RegexCompilationInfo object when you created it. Furthermore, each of these classes inherits from the Regex class so all the Regex methods are available for you to use. As you can see, creating a library of commonly used regular expressions allows you to reuse and share these expressions in a multitude of different projects. A change in a regular expression simply involves changing one assembly instead of each project that hard-coded the regular expression.