日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

.NET中的正则表达式 (三)RegexCompilationInfo 类

發布時間:2023/12/13 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .NET中的正则表达式 (三)RegexCompilationInfo 类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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? ?

轉載于:https://www.cnblogs.com/dyufei/archive/2010/08/14/2573922.html

總結

以上是生活随笔為你收集整理的.NET中的正则表达式 (三)RegexCompilationInfo 类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。