[ 转]C# 中使用正则表达式 Regex.Matches方法的几个应用
2013-04-09 21:21
671 查看
本文转自:http://blog.csdn.net/gishero/article/details/5244463
用于正则表达式的 Regex.Matches静态方法的几种用法:
[c-sharp] view plaincopyprint?
//①正则表达式 = > 匹配字符串
string Text = @"This is a book , this is my book , Is not IIS";
//定义一个模式字符串,不仅仅是纯文本,还可以是正则表达式
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("匹配的内容:{0,2} ", NextMatch.Value);
Console.Write("/n");
}
Console.WriteLine();
//②匹配以大写I开头
//“/b”是转义序列,代表开头和结尾(一个字的边界,忽略空白或标点)
Pattern = @"/bI";
Matches = Regex.Matches(
Text,
Pattern,
RegexOptions.ExplicitCapture //提高检索效率
);
Console.WriteLine("从左向右匹配字符串:");
foreach (Match NextMatch in Matches)
{
Console.Write("匹配的位置:{0} ", NextMatch.Index);
Console.Write("匹配的内容:{0} ", NextMatch.Value);
Console.Write("/n");
}
Console.WriteLine();
//③匹配以大写I开头,大写S结尾的字符串
//“/b”是转义序列,代表开头和结尾(一个字的边界,忽略空白或标点)
///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("匹配的内容:{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("匹配的内容:{0} ", NextMatch.Value);
Console.Write("/n");
}
Console.WriteLine();
//⑤对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("匹配的内容:{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();
用于正则表达式的 Regex.Matches静态方法的几种用法:
[c-sharp] view plaincopyprint?
//①正则表达式 = > 匹配字符串
string Text = @"This is a book , this is my book , Is not IIS";
//定义一个模式字符串,不仅仅是纯文本,还可以是正则表达式
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("匹配的内容:{0,2} ", NextMatch.Value);
Console.Write("/n");
}
Console.WriteLine();
//②匹配以大写I开头
//“/b”是转义序列,代表开头和结尾(一个字的边界,忽略空白或标点)
Pattern = @"/bI";
Matches = Regex.Matches(
Text,
Pattern,
RegexOptions.ExplicitCapture //提高检索效率
);
Console.WriteLine("从左向右匹配字符串:");
foreach (Match NextMatch in Matches)
{
Console.Write("匹配的位置:{0} ", NextMatch.Index);
Console.Write("匹配的内容:{0} ", NextMatch.Value);
Console.Write("/n");
}
Console.WriteLine();
//③匹配以大写I开头,大写S结尾的字符串
//“/b”是转义序列,代表开头和结尾(一个字的边界,忽略空白或标点)
///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("匹配的内容:{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("匹配的内容:{0} ", NextMatch.Value);
Console.Write("/n");
}
Console.WriteLine();
//⑤对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("匹配的内容:{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();
[c-sharp] view plaincopyprint? ①从右向左匹配字符串: 匹配的位置:43 匹配的内容:IS 匹配的位置:35 匹配的内容:Is 匹配的位置:22 匹配的内容:is 匹配的位置:19 匹配的内容:is 匹配的位置: 5 匹配的内容:is 匹配的位置: 2 匹配的内容:is ②从左向右匹配字符串: 匹配的位置:35 匹配的内容:I 匹配的位置:42 匹配的内容:I ③从左向右匹配字符串: 匹配的位置:42 匹配的内容:IIS ④从左向右匹配字符串: 匹配的位置:1 匹配的内容:his 匹配的位置:18 匹配的内容:his 匹配的位置:42 匹配的内容:IIS ⑤从左向右匹配字符串: 匹配的位置:0 匹配的内容:http://192.168.0.1:2008 匹配的组 1:http://192.168.0.1:2008 匹配的组 2:http 匹配的组 3:192.168.0.1 匹配的组 4:2008 ①从右向左匹配字符串: 匹配的位置:43 匹配的内容:IS 匹配的位置:35 匹配的内容:Is 匹配的位置:22 匹配的内容:is 匹配的位置:19 匹配的内容:is 匹配的位置: 5 匹配的内容:is 匹配的位置: 2 匹配的内容:is ②从左向右匹配字符串: 匹配的位置:35 匹配的内容:I 匹配的位置:42 匹配的内容:I ③从左向右匹配字符串: 匹配的位置:42 匹配的内容:IIS ④从左向右匹配字符串: 匹配的位置:1 匹配的内容:his 匹配的位置:18 匹配的内容:his 匹配的位置:42 匹配的内容:IIS ⑤从左向右匹配字符串: 匹配的位置:0 匹配的内容:http://192.168.0.1:2008 匹配的组 1:http://192.168.0.1:2008 匹配的组 2:http 匹配的组 3:192.168.0.1 匹配的组 4:2008
相关文章推荐
- C# 中使用正则表达式 Regex.Matches方法的几个应用[转]
- C# 中使用正则表达式 Regex.Matches方法的几个应用
- C#中正则表达式Regex的match和matches方法
- Delphi正则表达式使用方法(TPerlRegEx)
- C# Regex正则常用方法的使用
- Delphi正则表达式使用方法(TPerlRegEx)
- Delphi正则表达式使用方法(TPerlRegEx)
- C#验证控件使用方法及常用正则表达式例析
- 正则表达式语法规则及在Javascript和C#中的使用方法
- 正则表达式(regex) 贪婪模式、懒惰模式使用方法
- 正则表达式基础应用(匹配matches(regex))
- C#正则表达式使用方法示例
- C#常用正则表达式验证(使用C#3.0扩展方法)(转)
- C#正则表达式使用方法
- C#中使用正则表达式提取超链接地址的集中方法
- ExtJS4的文本框(textField)使用正则表达式进行验证(Regex)的方法
- C#正则表达式(RegEx)高级应用之分组(Group)替换(Replace)
- C#正则表达式(RegEx)高级应用之分组(Group)替换(Replace)
- .net正则表达式大全(.net 的 System.Text.RegularExpressions.Regex.Match()方法使用)
- .net正则表达式大全(.net 的 System.Text.RegularExpressions.Regex.Match()方法使用)