用于正则表达式的 Regex.Matches静态方法的几种用法 转载
2013-05-27 16:13
549 查看
//①正则表达式 = > 匹配字符串 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静态方法的几种用法
- 用于过滤空白字符的几种常见的js正则表达式pattern
- 【备忘】用于过滤空白字符的几种常见的正则表达式pattern——在JavaScript中的表现形式
- js正则表达式中的问号几种用法小结
- iOS 正则表达式 及 几种用法
- Python正则表达式的几种匹配用法
- java中Pattern与Matcher之间的用法,正则表达式,用于拆分语句
- [转载]强大的grep用法详解:grep与正则表达式
- 用于过滤空白字符的几种常见的js正则表达式pattern
- js正则表达式中的问号几种用法小结
- js正则表达式中的问号几种用法小结
- Java中正则表达式的几种用法
- 转载:Python正则表达式的用法(1. 匹配)
- Java中正则表达式的几种用法
- 转载:Python正则表达式的用法(2. 替换;3.拆分)
- Python正则表达式的几种匹配用法
- Python正则表达式的几种匹配用法
- C#正则表达式编程(二):Regex类用法
- PYTHON-正则表达式用法
- ORACLE正则表达式函数详解--转载整理