This documentation is archived and is not being maintained.

RegexCompilationInfo Constructor

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.

Each parameter of the RegexCompilationInfo 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 do not have to 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.

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

*/

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Show: