日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

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

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

RegexCompilationInfo 類

提供編譯器用于將正則表達(dá)式編譯為獨(dú)立程序集的信息。

屬性

IsPublic:獲取或設(shè)置一個(gè)值,該值指示所編譯的正則表達(dá)式是否具有公共可見性。

Name:獲取或設(shè)置用于所編譯的正則表達(dá)式的類型名稱。

Namespace:獲取或設(shè)置要將新類型添加到的命名空間。

Options:獲取或設(shè)置編譯正則表達(dá)式時(shí)使用的編譯器選項(xiàng)。

Pattern:獲取或設(shè)置要編譯的正則表達(dá)式。

方法

Equals:已重載。 確定兩個(gè) Object 實(shí)例是否相等。 (從 Object 繼承。)

GetHashCode:用作特定類型的哈希函數(shù)。GetHashCode 適合在哈希算法和數(shù)據(jù)結(jié)構(gòu)(如哈希表)中使用。 (從 Object 繼承。)

GetType:獲取當(dāng)前實(shí)例的 Type。 (從 Object 繼承。)

ReferenceEquals:確定指定的 Object 實(shí)例是否是相同的實(shí)例。 (從 Object 繼承。)

ToString:返回表示當(dāng)前 Object 的 String。 (從 Object 繼承。)

示例

面的代碼示例通過三個(gè)步驟定義、創(chuàng)建和使用編譯過的正則表達(dá)式。

第一個(gè)步驟將編譯下面的代碼示例。代碼示例中的 RegexCompilationInfo 構(gòu)造函數(shù)準(zhǔn)備了一個(gè)正則表達(dá)式以供編譯

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

第二步:運(yùn)行第一個(gè)步驟中編譯的可執(zhí)行文件。該可執(zhí)行文件創(chuàng)建 FishRegex.dll 程序集以及一個(gè)名為 FishRegex 的編譯過的正則表達(dá)式類型。

第三步:使用對(duì) FishRegex.dll 的引用編譯下面的代碼示例,然后運(yùn)行得到的可執(zhí)行文件。該可執(zhí)行文件使用 FishRegex 類型對(duì)目標(biāo)字符串進(jìn)行匹配,并顯示匹配項(xiàng)、組、捕獲組以及匹配項(xiàng)在目標(biāo)字符串中的索引位置。

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

轉(zhuǎn)載于:https://www.cnblogs.com/dyufei/archive/2010/08/14/2573922.html

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。