用于正则表达式的 Regex.Matches静态方法的几种用法
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .net访问PostgreSQL数据库发
- 下一篇: Method Overloading