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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用于正则表达式的 Regex.Matches静态方法的几种用法

發(fā)布時(shí)間:2024/10/8 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用于正则表达式的 Regex.Matches静态方法的几种用法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

view plaincopy to clipboardprint?
//①正則表達(dá)式 = > 匹配字符串??
string Text = @"This is a book , this is my book , Is not IIS";??
?
//定義一個(gè)模式字符串,不僅僅是純文本,還可以是正則表達(dá)式??
string Pattern = "is";??
?
MatchCollection Matches = Regex.Matches(??
??? Text,??
??? Pattern,??
??? RegexOptions.IgnoreCase |???????? //忽略大小寫??
??? RegexOptions.ExplicitCapture |??? //提高檢索效率??
??? RegexOptions.RightToLeft????????? //從左向右匹配字符串??
??? );??
?
Console.WriteLine("從右向左匹配字符串:");??
?
foreach (Match NextMatch in Matches)??
{?????????????????
??? Console.Write("匹配的位置:{0,2} ", NextMatch.Index);??
??? Console.Write("匹配的內(nèi)容:{0,2} ", NextMatch.Value);??
??? Console.Write("\n");?????
}??
?
Console.WriteLine();??
?
//②匹配以大寫I開頭??
//“\b”是轉(zhuǎn)義序列,代表開頭和結(jié)尾(一個(gè)字的邊界,忽略空白或標(biāo)點(diǎn))??
Pattern = @"\bI";??
Matches = Regex.Matches(??
??? Text,??
??? Pattern,??
??? RegexOptions.ExplicitCapture??? //提高檢索效率??
??? );??
?
Console.WriteLine("從左向右匹配字符串:");??
?
foreach (Match NextMatch in Matches)??
{??
??? Console.Write("匹配的位置:{0} ", NextMatch.Index);??
??? Console.Write("匹配的內(nèi)容:{0} ", NextMatch.Value);??
??? Console.Write("\n");??
}??
?
Console.WriteLine();??
?
//③匹配以大寫I開頭,大寫S結(jié)尾的字符串??
//“\b”是轉(zhuǎn)義序列,代表開頭和結(jié)尾(一個(gè)字的邊界,忽略空白或標(biāo)點(diǎn))??
//\S*匹配任何不是空白的字符??
Pattern = @"\bI\S*S\b";??
Matches = Regex.Matches(??
??? Text,??
??? Pattern,??
??? RegexOptions.ExplicitCapture??? //提高檢索效率??
??? );??
?
Console.WriteLine("從左向右匹配字符串:");??
?
foreach (Match NextMatch in Matches)??
{??
??? Console.Write("匹配的位置:{0} ", NextMatch.Index);??
??? Console.Write("匹配的內(nèi)容:{0} ", NextMatch.Value);??
??? Console.Write("\n");??
}??
?
Console.WriteLine();??
?
//④匹配his 或者iis,其中忽略大小寫??
Pattern = @"[h|i]is";??
Matches = Regex.Matches(??
??? Text,??
??? Pattern,??
??? RegexOptions.IgnoreCase |???????? //忽略大小寫??
??? RegexOptions.ExplicitCapture??? //提高檢索效率??
??? );??
?
Console.WriteLine("從左向右匹配字符串:");??
?
foreach (Match NextMatch in Matches)??
{??
??? Console.Write("匹配的位置:{0} ", NextMatch.Index);??
??? Console.Write("匹配的內(nèi)容:{0} ", NextMatch.Value);??
??? Console.Write("\n");??????????????
}??
?
Console.WriteLine();??
?
//⑤對(duì)Url的分組匹配??
Text = "http://192.168.0.1:2008";??
Pattern = @"\b(\S+)://(\S+)(?::(\S+))\b";??
Matches = Regex.Matches(Text, Pattern);??
?
Console.WriteLine("從左向右匹配字符串:");??
?
foreach (Match NextMatch in Matches)??
{??
??? Console.Write("匹配的位置:{0} ", NextMatch.Index);??
??? Console.Write("匹配的內(nèi)容:{0} ", NextMatch.Value);??
??? Console.Write("\n");??
?
??? for (int i = 0; i < NextMatch.Groups.Count; i++)??
??? {??
??????? Console.Write("匹配的組 {0}:{1,4} ", i + 1, NextMatch.Groups[i].Value);??
??????? Console.Write("\n");??
??? }??
}??
?
Console.Read();?
??????????? //①正則表達(dá)式 = > 匹配字符串
??????????? string Text = @"This is a book , this is my book , Is not IIS";

??????????? //定義一個(gè)模式字符串,不僅僅是純文本,還可以是正則表達(dá)式
??????????? string Pattern = "is";

??????????? MatchCollection Matches = Regex.Matches(
??????????????? Text,
??????????????? Pattern,
??????????????? RegexOptions.IgnoreCase |???????? //忽略大小寫
??????????????? RegexOptions.ExplicitCapture |??? //提高檢索效率
??????????????? RegexOptions.RightToLeft????????? //從左向右匹配字符串
??????????????? );

??????????? Console.WriteLine("從右向左匹配字符串:");

??????????? foreach (Match NextMatch in Matches)
??????????? {??????????????
??????????????? Console.Write("匹配的位置:{0,2} ", NextMatch.Index);
??????????????? Console.Write("匹配的內(nèi)容:{0,2} ", NextMatch.Value);
??????????????? Console.Write("\n");??
??????????? }

??????????? Console.WriteLine();

??????????? //②匹配以大寫I開頭
??????????? //“\b”是轉(zhuǎn)義序列,代表開頭和結(jié)尾(一個(gè)字的邊界,忽略空白或標(biāo)點(diǎn))
??????????? Pattern = @"\bI";
??????????? Matches = Regex.Matches(
??????????????? Text,
??????????????? Pattern,
??????????????? RegexOptions.ExplicitCapture??? //提高檢索效率
??????????????? );

??????????? Console.WriteLine("從左向右匹配字符串:");

??????????? foreach (Match NextMatch in Matches)
??????????? {
??????????????? Console.Write("匹配的位置:{0} ", NextMatch.Index);
??????????????? Console.Write("匹配的內(nèi)容:{0} ", NextMatch.Value);
??????????????? Console.Write("\n");
??????????? }

??????????? Console.WriteLine();

??????????? //③匹配以大寫I開頭,大寫S結(jié)尾的字符串
??????????? //“\b”是轉(zhuǎn)義序列,代表開頭和結(jié)尾(一個(gè)字的邊界,忽略空白或標(biāo)點(diǎn))
??????????? //\S*匹配任何不是空白的字符
??????????? Pattern = @"\bI\S*S\b";
??????????? Matches = Regex.Matches(
??????????????? Text,
??????????????? Pattern,
??????????????? RegexOptions.ExplicitCapture??? //提高檢索效率
??????????????? );

??????????? Console.WriteLine("從左向右匹配字符串:");

??????????? foreach (Match NextMatch in Matches)
??????????? {
??????????????? Console.Write("匹配的位置:{0} ", NextMatch.Index);
??????????????? Console.Write("匹配的內(nèi)容:{0} ", NextMatch.Value);
??????????????? Console.Write("\n");
??????????? }

??????????? Console.WriteLine();

??????????? //④匹配his 或者iis,其中忽略大小寫
??????????? Pattern = @"[h|i]is";
??????????? Matches = Regex.Matches(
??????????????? Text,
??????????????? Pattern,
??????????????? RegexOptions.IgnoreCase |???????? //忽略大小寫
??????????????? RegexOptions.ExplicitCapture??? //提高檢索效率
??????????????? );

??????????? Console.WriteLine("從左向右匹配字符串:");

??????????? foreach (Match NextMatch in Matches)
??????????? {
??????????????? Console.Write("匹配的位置:{0} ", NextMatch.Index);
??????????????? Console.Write("匹配的內(nèi)容:{0} ", NextMatch.Value);
??????????????? Console.Write("\n");???????????
??????????? }

??????????? Console.WriteLine();

??????????? //⑤對(duì)Url的分組匹配
??????????? Text = "http://192.168.0.1:2008";
??????????? Pattern = @"\b(\S+)://(\S+)(?::(\S+))\b";
??????????? Matches = Regex.Matches(Text, Pattern);

??????????? Console.WriteLine("從左向右匹配字符串:");

??????????? foreach (Match NextMatch in Matches)
??????????? {
??????????????? Console.Write("匹配的位置:{0} ", NextMatch.Index);
??????????????? Console.Write("匹配的內(nèi)容:{0} ", NextMatch.Value);
??????????????? Console.Write("\n");

??????????????? for (int i = 0; i < NextMatch.Groups.Count; i++)
??????????????? {
??????????????????? Console.Write("匹配的組 {0}:{1,4} ", i + 1, NextMatch.Groups[i].Value);
??????????????????? Console.Write("\n");
??????????????? }
??????????? }

??????????? Console.Read();

輸出結(jié)果為:

view plaincopy to clipboardprint?
①從右向左匹配字符串:??
匹配的位置:43 匹配的內(nèi)容:IS???
匹配的位置:35 匹配的內(nèi)容:Is???
匹配的位置:22 匹配的內(nèi)容:is???
匹配的位置:19 匹配的內(nèi)容:is???
匹配的位置: 5 匹配的內(nèi)容:is???
匹配的位置: 2 匹配的內(nèi)容:is???
?
②從左向右匹配字符串:??
匹配的位置:35 匹配的內(nèi)容:I???
匹配的位置:42 匹配的內(nèi)容:I???
?
③從左向右匹配字符串:??
匹配的位置:42 匹配的內(nèi)容:IIS???
?
④從左向右匹配字符串:??
匹配的位置:1 匹配的內(nèi)容:his???
匹配的位置:18 匹配的內(nèi)容:his???
匹配的位置:42 匹配的內(nèi)容:IIS???
?
⑤從左向右匹配字符串:??
匹配的位置:0 匹配的內(nèi)容:http://192.168.0.1:2008???
匹配的組 1:http://192.168.0.1:2008???
匹配的組 2:http???
匹配的組 3:192.168.0.1???
匹配的組 4:2008??

轉(zhuǎn)載于:https://www.cnblogs.com/waters/archive/2011/05/20/Regex.html

總結(jié)

以上是生活随笔為你收集整理的用于正则表达式的 Regex.Matches静态方法的几种用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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