RegexCompilationInfo 类
提供编译器用于将正则表达式编译为独立程序集的信息。
属性
IsPublic:获取或设置一个值,该值指示所编译的正则表达式是否具有公共可见性。
Name:获取或设置用于所编译的正则表达式的类型名称。
Namespace:获取或设置要将新类型添加到的命名空间。
Options:获取或设置编译正则表达式时使用的编译器选项。
Pattern:获取或设置要编译的正则表达式。
方法
Equals:已重载。 确定两个 Object 实例是否相等。 (从 Object 继承。)
GetHashCode:用作特定类型的哈希函数。GetHashCode 适合在哈希算法和数据结构(如哈希表)中使用。 (从 Object 继承。)
GetType:获取当前实例的 Type。 (从 Object 继承。)
ReferenceEquals:确定指定的 Object 实例是否是相同的实例。 (从 Object 继承。)
ToString:返回表示当前 Object 的 String。 (从 Object 继承。)
示例
面的代码示例通过三个步骤定义、创建和使用编译过的正则表达式。
第一个步骤将编译下面的代码示例。代码示例中的 RegexCompilationInfo 构造函数准备了一个正则表达式以供编译
| 1 | // This code example demonstrates the RegexCompilationInfo constructor |
| 2 | // and the Regex.CompileToAssembly() method. |
| 3 | // compile: csc genFishRegex.cs |
| 4 | |
| 5 | namespace MyApp |
| 6 | { |
| 7 | using System; |
| 8 | using System.Reflection; |
| 9 | using System.Text.RegularExpressions; |
| 10 | class GenFishRegEx |
| 11 | { |
| 12 | public static void Main() |
| 13 | { |
| 14 | // Pattern = Group matches one or more word characters, |
| 15 | // one or more white space characters, |
| 16 | // group matches the string "fish". |
| 17 | string pat = @"(/w+)/s+(fish)"; |
| 18 | |
| 19 | // Create the compilation information. |
| 20 | // Case-insensitive matching; type name = "FishRegex"; |
| 21 | // namespace = "MyApp"; type is public. |
| 22 | RegexCompilationInfo rci = new RegexCompilationInfo( |
| 23 | pat, RegexOptions.IgnoreCase, |
| 24 | "FishRegex", "MyApp", true); |
| 25 | |
| 26 | // Setup to compile. |
| 27 | AssemblyName an = new AssemblyName(); |
| 28 | an.Name = "FishRegex"; |
| 29 | RegexCompilationInfo[] rciList = { rci }; |
| 30 | |
| 31 | // Compile the regular expression. |
| 32 | Regex.CompileToAssembly(rciList, an); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | This code example produces the following results: |
| 39 | |
| 40 | (Execute this code to generate the compiled regular |
| 41 | expression assembly named FishRegex.dll. |
| 42 | Use FishRegex.dll as a reference when compiling |
| 43 | useFishRegex.cs.) |
| 44 | |
| 45 | */ |
| 46 |
第二步:运行第一个步骤中编译的可执行文件。该可执行文件创建 FishRegex.dll 程序集以及一个名为 FishRegex 的编译过的正则表达式类型。
第三步:使用对 FishRegex.dll 的引用编译下面的代码示例,然后运行得到的可执行文件。该可执行文件使用 FishRegex 类型对目标字符串进行匹配,并显示匹配项、组、捕获组以及匹配项在目标字符串中的索引位置。
| 1 | // This code example demonstrates the RegexCompilationInfo constructor. |
| 2 | // Execute this code example after executing genFishRegex.exe. |
| 3 | // compile: csc /r:FishRegex.dll useFishRegex.cs |
| 4 | |
| 5 | namespace MyApp |
| 6 | { |
| 7 | using System; |
| 8 | using System.Reflection; |
| 9 | using System.Text.RegularExpressions; |
| 10 | |
| 11 | class UseFishRegEx |
| 12 | { |
| 13 | public static void Main() |
| 14 | { |
| 15 | // Match against the following target string. |
| 16 | string targetString = "One fish two fish red fish blue fish"; |
| 17 | int matchCount = 0; |
| 18 | FishRegex f = new FishRegex(); |
| 19 | |
| 20 | // Display the target string. |
| 21 | Console.WriteLine("/nInput string = /"" + targetString + "/""); |
| 22 | |
| 23 | // Display each match, capture group, capture, and match position. |
| 24 | foreach (Match m in f.Matches(targetString)) |
| 25 | { |
| 26 | Console.WriteLine("/nMatch(" + (++matchCount) + ")"); |
| 27 | for (int i = 1; i <= 2; i++) |
| 28 | { |
| 29 | Group g = m.Groups[i]; |
| 30 | Console.WriteLine("Group(" + i + ") = /"" + g + "/""); |
| 31 | CaptureCollection cc = g.Captures; |
| 32 | for (int j = 0; j < cc.Count; j++) |
| 33 | { |
| 34 | Capture c = cc[j]; |
| 35 | System.Console.WriteLine( |
| 36 | "Capture(" + j + ") = /"" + c + "/", Position = " + c.Index); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | This code example produces the following results: |
| 46 | |
| 47 | Input string = "One fish two fish red fish blue fish" |
| 48 | |
| 49 | Match(1) |
| 50 | Group(1) = "One" |
| 51 | Capture(0) = "One", Position = 0 |
| 52 | Group(2) = "fish" |
| 53 | Capture(0) = "fish", Position = 4 |
| 54 | |
| 55 | Match(2) |
| 56 | Group(1) = "two" |
| 57 | Capture(0) = "two", Position = 9 |
| 58 | Group(2) = "fish" |
| 59 | Capture(0) = "fish", Position = 13 |
| 60 | |
| 61 | Match(3) |
| 62 | Group(1) = "red" |
| 63 | Capture(0) = "red", Position = 18 |
| 64 | Group(2) = "fish" |
| 65 | Capture(0) = "fish", Position = 22 |
| 66 | |
| 67 | Match(4) |
| 68 | Group(1) = "blue" |
| 69 | Capture(0) = "blue", Position = 27 |
| 70 | Group(2) = "fish" |
| 71 | Capture(0) = "fish", Position = 32 |
| 72 | |
| 73 | */ |
| 74 |
浙公网安备 33010602011771号