RegexCompilationInfo 类

本文详细介绍了正则表达式编译器提供的信息,包括如何将正则表达式编译为独立程序集,以及如何通过三个步骤定义、创建和使用编译过的正则表达式。此外,还提供了代码示例来展示整个过程。

RegexCompilationInfo 类

提供编译器用于将正则表达式编译为独立程序集的信息。

 

属性

名称

说明

IsPublic

获取或设置一个值,该值指示所编译的正则表达式是否具有公共可见性。

Name

获取或设置用于所编译的正则表达式的类型名称。

Namespace

获取或设置要将新类型添加到的命名空间。

Options

获取或设置编译正则表达式时使用的编译器选项。

Pattern

获取或设置要编译的正则表达式。

 

方法

名称

说明

Equals 

已重载。 确定两个 Object 实例是否相等。 (从 Object 继承。)

GetHashCode 

用作特定类型的哈希函数。GetHashCode 适合在哈希算法和数据结构(如哈希表)中使用。 (从 Object 继承。)

GetType 

获取当前实例的 Type。 (从 Object 继承。)

ReferenceEquals 

确定指定的 Object 实例是否是相同的实例。 (从 Object 继承。)

ToString 

返回表示当前 Object  String。 (从 Object 继承。)

 

示例

面的代码示例通过三个步骤定义、创建和使用编译过的正则表达式。

第一个步骤将编译下面的代码示例。代码示例中的 RegexCompilationInfo 构造函数准备了一个正则表达式以供编译

 

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

namespaceMyApp
{
   
using System;
   
using System.Reflection;
   
using System.Text.RegularExpressions;
   
class GenFishRegEx
    {
       
public static void Main()
        {
// Pattern = Group matches one or more wordcharacters,
//          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 ispublic.
        RegexCompilationInfo rci =
newRegexCompilationInfo(
                    pat,RegexOptions.IgnoreCase,
                   
"FishRegex", "MyApp", true);

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

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

/*
This code example produces the following results:

(Execute
thiscode to generate the compiled regular
expression assembly named FishRegex.dll.
Use FishRegex.dll as a reference when compiling
useFishRegex.cs.)

*/

 

 

第二个步骤运行第一个步骤中编译的可执行文件。该可执行文件创建FishRegex.dll 程序集以及一个名为 FishRegex 的编译过的正则表达式类型。

第三个步骤使用对FishRegex.dll 的引用编译下面的代码示例,然后运行得到的可执行文件。该可执行文件使用 FishRegex类型对目标字符串进行匹配,并显示匹配项、组、捕获组以及匹配项在目标字符串中的索引位置。

 

 

// This code example demonstrates the RegexCompilationInfoconstructor.
// Execute this code example after executinggenFishRegex.exe.
// compile: csc /r:FishRegex.dlluseFishRegex.cs

namespaceMyApp
  {
 
using System;
 
using System.Reflection;
 
using System.Text.RegularExpressions;

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

// 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 bluefish"

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

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值