Regex類

來源:互聯網
上載者:User
System.Text.RegularExpressions 命名空間提供Regex功能,主要包括7個類。他們的關係如右圖,對於他們的功能很多書和網上資料寫的很複雜,其實理一理還是很簡單的。我分了四部分來定義:

 定義Regex:
Regex類
返回匹配結果:
MatchCollection類 和 Match類
返回擷取的群組的結果:
GropCollection類 和 Group類
返回執行捕獲的結果結果:
CaptureCollection類 和 Captrue類
同時後三個相對的都是集合與子集的關係了。

 

 

Regex類的方法和屬性都很簡單,主要是對建構函式的運用不同,也不容易混淆,就不多言了。

Match類 可以使用Regex類的Match方法返回一個Match類對象。此時返回的時輸入字串的第一個匹配項,同時好多書中也沒有提到NextMatch,而使很多人都以為使用該類的對象只能匹配第一項,其實可以遍曆所有的匹配項。先前也,沒查什麼資料自己試著便利了一下,剛想起來看一下官方資料,一看和我的思路還是一樣的,只不過我還想了一下,要是直接看的話還不用花時間想。

 

usingSystem;
usingSystem.Text.RegularExpressions;
namespacetMatch
{
classProgram
{
publicstaticvoidMain(string[]args)
{
string s="123abc456abcdabc123abc";
Regexr=newRegex("abc");
Matchm=r.Match(s);
inti=1;
while(m.Success)
{
Console.WriteLine("第{0}個匹配項,匹配值{1}-匹配索引{2}",i,m.Value,m.Index);
m=m.NextMatch();
++i;
}
Console.ReadKey(true);
}
}
}

 

MatchCollection 類表示通過以迭代方式將Regex模式應用於輸入字串所找到的成功匹配的集合。通過通過對 Regex.Matches 方法的特定調用獲得。 在訪問集合的 Count 屬性(這個屬性在Match類中是沒有的)時,可使用該技術。 該技術填充集合的方法通常開銷較大,會對效能造成較大的影響。那面上面的就可以簡化了的使用遍曆 Regex.Matches返回的對象了。
usingSystem;
usingSystem.Text.RegularExpressions;
namespacetMatchCollection
{
classProgram
{
publicstaticvoidMain(string[]args)
{
string s="123abc456abcdabc123abc";
MatchCollectionmc=Regex.Matches(s,"abc");
//遍曆匹配項
for(inti=0;i<mc.Count;i++)
{
Console.WriteLine("第{0}個匹配項,匹配值{1}-匹配索引{2}",i,mc[i].Value,mc[i].Index);
}
Console.ReadKey(true);
}

}
}
靜態 Matches 方法等效於使用指定的Regex模式構造 Regex 對象並調用執行個體方法 Matches。等效於:
Match match = regex.Match(input, startAt);
while (match.Success) 
 { 
// Handle match here...
 match = match.NextMatch();
 } 
 Group類和GropCollection類 都是為了返回單個的擷取的群組或者擷取的群組的集合類,所以你首Crowdsourced Security Testing道什麼是擷取的群組,否則會濫用這個類,Match類的Groups方法擷取由Regex匹配的組的集合。也就是返回GroupCollection類的執行個體。例如:

 

using System;
using System.Text.RegularExpressions;
namespace tMatchCollection
{
class Program
{
public static void Main(string[] args)
{
string input="123abc456abcdabc123abc";
string pattern="(a(b))c"; //此處定義了三個擷取的群組
Match m=Regex.Match(input,pattern);
GroupCollection gc=m.Groups;//擷取由Regex匹配的組的集合
int n=1;
while(m.Success)
{
Console.WriteLine("第{0}個匹配項,匹配值{1}-匹配索引{2}",n+1,m.Value,m.Index);
m=m.NextMatch();
++n;
}
Console.WriteLine("捕獲到的組數:{0}",gc.Count);
//我們來查看包含哪些擷取的群組
for(int i=0;i<gc.Count;i++)
{
Console.WriteLine("{0}:組{1}-起始索引{2}",i+1,gc[i].Value,gc[i].Index);
}
Console.ReadKey(true);
}
}
}

 

這裡我就不再贅述Group類了,因為上面的例子裡已經隱性的使用了Group類的執行個體。Group類的執行個體是由Match.Groups(groupname)屬性返回的,或者是在使用(?<groupname>...)分組構造時,Match.Groups("groupname")屬性返回的。

CaptureCollection類表示一個擷取的群組做出的捕獲的集合,簡單說也就是執行一個擷取的群組返回一個或多給字串。
注意的一點就是它返回的是單個擷取的群組執行的捕獲結果。例如:

using System;
using System.Text.RegularExpressions;
namespace test
{
class Program
{
public static void Main(string[] args)
{
string input="XYZAbcAbcAbcXYZAbcAb";
string pattern="(Abc)+";
const string posinfo="開始於第{0}個字元";
Match m=Regex.Match(input,pattern);
GroupCollection gc=m.Groups;//擷取由Regex匹配的組的集合
Console.WriteLine("包含{0}個擷取的群組\n",gc.Count);
//我們來查看包含哪些擷取的群組
for(int i=0;i<gc.Count;i++)
{
CaptureCollection cc=gc[i].Captures;//返回單個擷取的群組執行的捕獲結果
int Cnum=cc.Count;//每一組執行捕獲的總數量
Console.WriteLine("{0}組執行捕獲的數量為:{1}",gc[i].Value,cc.Count);
//現在我們查看執行捕獲的具體資訊
for(int ii=0;ii<Cnum;ii++)
{
Console.WriteLine(cc[ii].Value+posinfo,cc[ii].Index);
}
Console.WriteLine();
}
Console.ReadKey(true);
}
}
}

同上也不再贅述Capture類,例子裡也已經隱性的使用了Capture類的執行個體。

    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.