RegexCompilationInfo Constructor (String, RegexOptions, String, String, Boolean)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Initializes a new instance of the RegexCompilationInfo class that contains information about a regular expression to be included in an assembly.

Namespace:   System.Text.RegularExpressions
Assembly:  System (in System.dll)

public RegexCompilationInfo(
	string pattern,
	RegexOptions options,
	string name,
	string fullnamespace,
	bool ispublic
)

Parameters

pattern
Type: System.String

The regular expression to compile.

options
Type: System.Text.RegularExpressions.RegexOptions

The regular expression options to use when compiling the regular expression.

name
Type: System.String

The name of the type that represents the compiled regular expression.

fullnamespace
Type: System.String

The namespace to which the new type belongs.

ispublic
Type: System.Boolean

true to make the compiled regular expression publicly visible; otherwise, false.

Exception Condition
ArgumentException

name is String.Empty.

ArgumentNullException

pattern is null.

-or-

name is null.

-or-

fullnamespace is null.

Each parameter of the RegexCompilationInfo(String, RegexOptions, String, String, Boolean) constructor directly corresponds to a property of the RegexCompilationInfo class. Because all properties are read/write, their values can also be assigned directly.

The CompileToAssembly method generates an assembly that contains compiled regular expressions. Therefore, you should not specify Compiled as one of the values of options.

If ispublic is true, the compiled regular expression class is given public accessibility. That is, it can be instantiated from code that executes in any assembly. If ispublic is false, the compiled regular expression class is given internal (in C#) or Friend (in Visual Basic) accessibility. That is, it can be instantiated only from code that executes in the same assembly as the regular expression class.

Notes to Callers:

This constructor creates a compiled regular expression that uses the default time-out value of the application domain in which it is created. If a time-out value is defined for the application domain, the compiled regular expression uses the value Regex.InfiniteMatchTimeout, which prevents a pattern-matching operation from timing out. The recommended constructor for creating a compiled regular expression is RegexCompilationInfo(String, RegexOptions, String, String, Boolean, TimeSpan), which lets you set the time-out interval.

The following example creates and uses a compiled regular expression in two steps.

In the first step, compile and execute the following code example. The RegexCompilationInfo constructor in the code example defines a compiled regular expression. The result of executing the code is an assembly named FishRegex.dll that contains a compiled regular expression type named FishRegex.

// This code example demonstrates the RegexCompilationInfo constructor
// and the Regex.CompileToAssembly() method.
// compile: csc genFishRegex.cs

namespace MyApp
{
    using System;
    using System.Reflection;
    using System.Text.RegularExpressions;
    class GenFishRegEx
    {
        public static void Main()
        {
// Pattern = Group matches one or more word characters, 
//           one or more white space characters, 
//           group matches the string "fish".
        string pat = @"(\w+)\s+(fish)";

// Create the compilation information.
// Case-insensitive matching; type name = "FishRegex"; 
// namespace = "MyApp"; type is public.
        RegexCompilationInfo rci = new RegexCompilationInfo(
                    pat, RegexOptions.IgnoreCase, 
                    "FishRegex", "MyApp", true);

// Setup to compile.
        AssemblyName an = new AssemblyName();
        an.Name = "FishRegex";
        RegexCompilationInfo[] rciList = { rci };

// Compile the regular expression.
        Regex.CompileToAssembly(rciList, an);
        }
    }
}

In the second step, compile the following code example using a reference to FishRegex.dll, and then run the resulting executable file. The executable file matches a target string by using the FishRegex type, and displays the match, group, capture group, and index position of the matches in the target string.

// This code example demonstrates the RegexCompilationInfo constructor.
// Execute this code example after executing genFishRegex.exe.
// compile: csc /r:FishRegex.dll useFishRegex.cs

namespace MyApp
  {
  using System;
  using System.Reflection;
  using System.Text.RegularExpressions;

  class UseFishRegEx
    {
    public static void Main()
      {
// Match against the following target string.
      string targetString = "One fish two fish red fish blue fish";
      int matchCount = 0;
      FishRegex f = new FishRegex();

// Display the target string.
      Console.WriteLine("\nInput string = \"" + targetString + "\"");

// Display each match, capture group, capture, and match position.
      foreach (Match m in f.Matches(targetString))
	{
	Console.WriteLine("\nMatch(" + (++matchCount) + ")");
	for (int i = 1; i <= 2; i++)
	  {
	  Group g = m.Groups[i];
	  Console.WriteLine("Group(" + i + ") = \"" + g + "\"");
	  CaptureCollection cc = g.Captures;
	  for (int j = 0; j < cc.Count; j++)
	    {
	    Capture c = cc[j];
	    System.Console.WriteLine(
	      "Capture(" + j + ") = \"" + c + "\", Position = " + c.Index);
	    }
	  }
	}
      }
    }
  }

/*
This code example produces the following results:

Input string = "One fish two fish red fish blue fish"

Match(1)
Group(1) = "One"
Capture(0) = "One", Position = 0
Group(2) = "fish"
Capture(0) = "fish", Position = 4

Match(2)
Group(1) = "two"
Capture(0) = "two", Position = 9
Group(2) = "fish"
Capture(0) = "fish", Position = 13

Match(3)
Group(1) = "red"
Capture(0) = "red", Position = 18
Group(2) = "fish"
Capture(0) = "fish", Position = 22

Match(4)
Group(1) = "blue"
Capture(0) = "blue", Position = 27
Group(2) = "fish"
Capture(0) = "fish", Position = 32

*/

.NET Framework
Available since 1.1
Return to top
Show: