Sunday, 15 April 2012

c# - How to handle large number of RegEx patterns when using Regex.CompileToAssembly? -


I am trying to compile a list of regular expressions in an assembly.

I've found that the 'name' value should be different for each RegexCompilationInfo objects, so when compiled they are different objects.

However, I have several regx patterns (as it is called about 100). Setting names on them and keeping names as it would be difficult not only that every time I want to check a text for each pattern (which I do), then I have to know all the names And each item must be used to use it)

Is there any easier way to handle it?

list & lt; RegexCompilationInfo & gt; Compile list = new list & amp ;;; RegexCompilationInfo & gt; (); CompilationList.Add (New RegexCompilationInfo (@ "Pattern 1", RegexOptions.CultureInvariant, "Name1", "Namespace Space.", True)); Compilation List Ad (New Regex Complanation Info (@ "Pattern 2", Regex Options. Culture Inventor, "Name 2", "Namespace Space", True)); ... ... collection list. Add (New Reggae Complanation Info (@ "Pattern 100", Reggae Options. Culture Inventor, "Name 100", "Namespace.Space", is True)); // Apply AssemblyTitle attribute to the new assembly // // Define the type of parameter (parameter) of AssemblyTitle attribute [] parameters = {typeof (string)}; // Define the title object of the Assembly [] paramValues ​​= {"compiled regular expression library"}; // ConstructorInfo ctor = typeof of the attribute (System.Reflection.AssemblyTitleAttribute), get the ConstructorInfo object representing the .GetConstructor (parameter); // CustomAttributeBuilder Object Object Custom Attributebuilder [] ATBILDER = {Create new custom-builtbubilder (CTOR, Ultimate valve)}; // Generate assembly with compiled regular expression RegexCompilationInfo [] CompilationRare = New RegexCompilationInfo [compilationList.Count]; Assembly name assemName = new assembly name ("Reggae Lib, version = 1.0.0.1001, culture = neutral, public token = zero"); CompilationList.CopyTo (compilationArray); Regex.CompileToAssembly (compilationarray, assemName, atbuilder);

Usage

  Name 1 name 1 regigix = new name1 (); Name 2 name 2 regigix = new name 2 (); ... .. (if name1Regex.Matches (text) .Qount> 0)  

To name the pattern, you can use only one incremental index. "name" + index .

You can use reflection to load all the patterns in a list

  var asm = assembly.Load ("RegexLib, version = 1.0.0.1001, Culture = neutral, public kokon = zero "); Var pattern = new list & lt; Reggae & gt; (); Var type (var type in asm.GetExportedTypes ()) {if (typeof (Regex) .IsAssignableFrom (type)} {patterns.Add (Regex) Activator.CreateInstance (type)); }} Return pattern;  

No comments:

Post a Comment