RegexCompilationInfo 构造函数

定义

初始化 RegexCompilationInfo 类的新实例。

重载

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

初始化 RegexCompilationInfo 类的新实例,该类包含有关要在程序集内包括的正则表达式的信息。

RegexCompilationInfo(String, RegexOptions, String, String, Boolean, TimeSpan)

初始化 RegexCompilationInfo 类的新实例,该类包含有关要在程序集内包括的指定的超时值的信息。

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

Source:
RegexCompilationInfo.cs
Source:
RegexCompilationInfo.cs
Source:
RegexCompilationInfo.cs

初始化 RegexCompilationInfo 类的新实例,该类包含有关要在程序集内包括的正则表达式的信息。

C#
public RegexCompilationInfo (string pattern, System.Text.RegularExpressions.RegexOptions options, string name, string fullnamespace, bool ispublic);

参数

pattern
String

要编译的正则表达式。

options
RegexOptions

编译正则表达式时使用的正则表达式选项。

name
String

表示已编译的正则表达式的类型名称。

fullnamespace
String

新类型所属的命名空间。

ispublic
Boolean

若要使所编译的正则表达式对公共可见,则为 true;否则,为 false

例外

nameEmpty

pattern 上声明的默认值为 null

name 上声明的默认值为 null

fullnamespace 上声明的默认值为 null

示例

以下示例通过两个步骤创建并使用已编译的正则表达式。

在第一步中,编译并执行以下代码示例。 RegexCompilationInfo代码示例中的构造函数定义编译的正则表达式。 执行代码的结果是一个名为 FishRegex.dll 的程序集,其中包含名为 的 FishRegex已编译正则表达式类型。

C#
// 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);
        }
    }
}

第二步使用对 FishRegex.dll 的引用编译以下代码示例,然后运行生成的可执行文件。 可执行文件使用 FishRegex 类型匹配目标字符串,并在目标字符串中显示匹配项的匹配项、组、捕获组和索引位置。

C#
// 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

*/

注解

构造函数的每个参数 RegexCompilationInfo(String, RegexOptions, String, String, Boolean) 直接对应于 类的 RegexCompilationInfo 一个属性。 由于所有属性都是可读/写的,因此也可以直接分配其值。

方法 CompileToAssembly 生成包含已编译正则表达式的程序集。 因此,不应将 指定 Compiled 为 的值之 options一。

如果 ispublictrue,则为编译的正则表达式类提供公共可访问性。 也就是说,可以从在任何程序集中执行的代码实例化它。 如果 ispublicfalse,则以 C#Friend) 或 Visual Basic) 辅助功能 ( (提供internal编译的正则表达式类。 也就是说,它只能从与正则表达式类在同一程序集中执行的代码实例化。

调用方说明

此构造函数创建一个已编译的正则表达式,该正则表达式使用创建它的应用程序域的默认超时值。 如果为应用程序域定义了超时值,则编译的正则表达式将使用 值 InfiniteMatchTimeout,这可以防止模式匹配操作超时。建议用于创建已编译正则表达式的构造函数是 RegexCompilationInfo(String, RegexOptions, String, String, Boolean, TimeSpan),这样就可以设置超时间隔。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

RegexCompilationInfo(String, RegexOptions, String, String, Boolean, TimeSpan)

Source:
RegexCompilationInfo.cs
Source:
RegexCompilationInfo.cs
Source:
RegexCompilationInfo.cs

初始化 RegexCompilationInfo 类的新实例,该类包含有关要在程序集内包括的指定的超时值的信息。

C#
public RegexCompilationInfo (string pattern, System.Text.RegularExpressions.RegexOptions options, string name, string fullnamespace, bool ispublic, TimeSpan matchTimeout);

参数

pattern
String

要编译的正则表达式。

options
RegexOptions

编译正则表达式时使用的正则表达式选项。

name
String

表示已编译的正则表达式的类型名称。

fullnamespace
String

新类型所属的命名空间。

ispublic
Boolean

若要使所编译的正则表达式对公共可见,则为 true;否则,为 false

matchTimeout
TimeSpan

正则表达式的默认超时间隔。

例外

nameEmpty

pattern 上声明的默认值为 null

name 上声明的默认值为 null

fullnamespacenull

matchTimeout 为负、零或大于 24 天左右。

示例

以下示例定义一个名为 DuplicateChars 的单个编译正则表达式,该正则表达式标识输入字符串中同一字符的两个或更多个匹配项。 编译的正则表达式的默认超时为 2 秒。 执行该示例时,它会创建一个名为 RegexLib.dll 的类库,其中包含编译的正则表达式。

C#
using System;
using System.Reflection;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
        // Match two or more occurrences of the same character.
        string pattern = @"(\w)\1+";
        
        // Use case-insensitive matching. 
        var rci = new RegexCompilationInfo(pattern, RegexOptions.IgnoreCase,
                                           "DuplicateChars", "CustomRegexes", 
                                           true, TimeSpan.FromSeconds(2));

        // Define an assembly to contain the compiled regular expression.
        var an = new AssemblyName();
        an.Name = "RegexLib";
        RegexCompilationInfo[] rciList = { rci };

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

正则表达式模式 (\w)\1+ 的定义如下表所示。

模式 说明
(\w) 匹配任意单词字符,并将其分配给第一个捕获组。
\1+ 匹配第一个捕获组的值的一个或多个匹配项。

以下示例使用 DuplicatedChars 正则表达式来标识字符串数组中的重复字符。 调用 DuplicatedChars 构造函数时,它会将超时间隔更改为 0.5 秒。

C#
using CustomRegexes;
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      var rgx = new DuplicateChars(TimeSpan.FromSeconds(.5));
      
      string[] values = { "Greeeeeat", "seed", "deed", "beam", 
                          "loop", "Aardvark" };
      // Display regex information.
      Console.WriteLine("Regular Expression Pattern: {0}", rgx);
      Console.WriteLine("Regex timeout value: {0} seconds\n", 
                        rgx.MatchTimeout.TotalSeconds);
      
      // Display matching information.
      foreach (var value in values) {
         Match m = rgx.Match(value);
         if (m.Success)
            Console.WriteLine("'{0}' found in '{1}' at positions {2}-{3}",
                              m.Value, value, m.Index, m.Index + m.Length - 1);
         else
            Console.WriteLine("No match found in '{0}'", value);
      }                                                         
   }
}
// The example displays the following output:
//       Regular Expression Pattern: (\w)\1+
//       Regex timeout value: 0.5 seconds
//       
//       //eeeee// found in //Greeeeeat// at positions 2-6
//       //ee// found in //seed// at positions 1-2
//       //ee// found in //deed// at positions 1-2
//       No match found in //beam//
//       //oo// found in //loop// at positions 1-2
//       //Aa// found in //Aardvark// at positions 0-1

注解

构造函数的每个参数 RegexCompilationInfo(String, RegexOptions, String, String, Boolean) 直接对应于 类的 RegexCompilationInfo 一个属性。 由于所有属性都是可读/写的,因此也可以直接分配其值。

方法 CompileToAssembly 生成包含已编译正则表达式的程序集。 因此,不应将 指定 Compiled 为 的值之 options一。

如果 ispublictrue,则为编译的正则表达式类提供公共可访问性。 也就是说,可以从在任何程序集中执行的代码实例化它。 如果 ispublicfalse,则以 C#Friend) 或 Visual Basic) 辅助功能 ( (提供internal编译的正则表达式类。 也就是说,它只能从与正则表达式类在同一程序集中执行的代码实例化。

参数 matchTimeout 定义已编译正则表达式的默认超时间隔。 此值表示编译的正则表达式对象在操作超时和正则表达式引擎在其下一次RegexMatchTimeoutException计时检查引发异常之前执行单个匹配操作的大致时间量。 有关超时值的其他信息,请参阅 MatchTimeout 属性。

重要

建议始终为已编译的正则表达式设置默认超时值。 正则表达式库的使用者可以通过将表示新超时间隔的值传递给 TimeSpan 此构造函数重载来替代该超时值。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1