[ 转]C# 中使用正则表达式 Regex.Matches方法的几个应用

本文转自:http://blog.csdn.net/gishero/article/details/5244463

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

 

  1. //①正则表达式 = > 匹配字符串   
  2. string Text = @"This is a book , this is my book , Is not IIS";  
  3.   
  4. //定义一个模式字符串,不仅仅是纯文本,还可以是正则表达式   
  5. string Pattern = "is";  
  6.   
  7. MatchCollection Matches = Regex.Matches(  
  8.     Text,  
  9.     Pattern,  
  10.     RegexOptions.IgnoreCase |         //忽略大小写   
  11.     RegexOptions.ExplicitCapture |    //提高检索效率   
  12.     RegexOptions.RightToLeft          //从左向右匹配字符串   
  13.     );  
  14.   
  15. Console.WriteLine("从右向左匹配字符串:");  
  16.   
  17. foreach (Match NextMatch in Matches)  
  18. {                 
  19.     Console.Write("匹配的位置:{0,2} ", NextMatch.Index);  
  20.     Console.Write("匹配的内容:{0,2} ", NextMatch.Value);  
  21.     Console.Write("/n");     
  22. }  
  23.   
  24. Console.WriteLine();  
  25.   
  26. //②匹配以大写I开头   
  27. //“/b”是转义序列,代表开头和结尾(一个字的边界,忽略空白或标点)   
  28. Pattern = @"/bI";  
  29. Matches = Regex.Matches(  
  30.     Text,  
  31.     Pattern,  
  32.     RegexOptions.ExplicitCapture    //提高检索效率   
  33.     );  
  34.   
  35. Console.WriteLine("从左向右匹配字符串:");  
  36.   
  37. foreach (Match NextMatch in Matches)  
  38. {  
  39.     Console.Write("匹配的位置:{0} ", NextMatch.Index);  
  40.     Console.Write("匹配的内容:{0} ", NextMatch.Value);  
  41.     Console.Write("/n");  
  42. }  
  43.   
  44. Console.WriteLine();  
  45.   
  46. //③匹配以大写I开头,大写S结尾的字符串   
  47. //“/b”是转义序列,代表开头和结尾(一个字的边界,忽略空白或标点)   
  48. ///S*匹配任何不是空白的字符   
  49. Pattern = @"/bI/S*S/b";  
  50. Matches = Regex.Matches(  
  51.     Text,  
  52.     Pattern,  
  53.     RegexOptions.ExplicitCapture    //提高检索效率   
  54.     );  
  55.   
  56. Console.WriteLine("从左向右匹配字符串:");  
  57.   
  58. foreach (Match NextMatch in Matches)  
  59. {  
  60.     Console.Write("匹配的位置:{0} ", NextMatch.Index);  
  61.     Console.Write("匹配的内容:{0} ", NextMatch.Value);  
  62.     Console.Write("/n");  
  63. }  
  64.   
  65. Console.WriteLine();  
  66.   
  67. //④匹配his 或者iis,其中忽略大小写   
  68. Pattern = @"[h|i]is";  
  69. Matches = Regex.Matches(  
  70.     Text,  
  71.     Pattern,  
  72.     RegexOptions.IgnoreCase |         //忽略大小写   
  73.     RegexOptions.ExplicitCapture    //提高检索效率   
  74.     );  
  75.   
  76. Console.WriteLine("从左向右匹配字符串:");  
  77.   
  78. foreach (Match NextMatch in Matches)  
  79. {  
  80.     Console.Write("匹配的位置:{0} ", NextMatch.Index);  
  81.     Console.Write("匹配的内容:{0} ", NextMatch.Value);  
  82.     Console.Write("/n");              
  83. }  
  84.   
  85. Console.WriteLine();  
  86.   
  87. //⑤对Url的分组匹配   
  88. Text = "http://192.168.0.1:2008";  
  89. Pattern = @"/b(/S+)://(/S+)(?::(/S+))/b";  
  90. Matches = Regex.Matches(Text, Pattern);  
  91.   
  92. Console.WriteLine("从左向右匹配字符串:");  
  93.   
  94. foreach (Match NextMatch in Matches)  
  95. {  
  96.     Console.Write("匹配的位置:{0} ", NextMatch.Index);  
  97.     Console.Write("匹配的内容:{0} ", NextMatch.Value);  
  98.     Console.Write("/n");  
  99.   
  100.     for (int i = 0; i < NextMatch.Groups.Count; i++)  
  101.     {  
  102.         Console.Write("匹配的组 {0}:{1,4} ", i + 1, NextMatch.Groups[i].Value);  
  103.         Console.Write("/n");  
  104.     }  
  105. }  
  106.   
  107. Console.Read();  

 

 

输出结果为:

  1. ①从右向左匹配字符串:  
  2. 匹配的位置:43 匹配的内容:IS   
  3. 匹配的位置:35 匹配的内容:Is   
  4. 匹配的位置:22 匹配的内容:is   
  5. 匹配的位置:19 匹配的内容:is   
  6. 匹配的位置: 5 匹配的内容:is   
  7. 匹配的位置: 2 匹配的内容:is   
  8.   
  9. ②从左向右匹配字符串:  
  10. 匹配的位置:35 匹配的内容:I   
  11. 匹配的位置:42 匹配的内容:I   
  12.   
  13. ③从左向右匹配字符串:  
  14. 匹配的位置:42 匹配的内容:IIS   
  15.   
  16. ④从左向右匹配字符串:  
  17. 匹配的位置:1 匹配的内容:his   
  18. 匹配的位置:18 匹配的内容:his   
  19. 匹配的位置:42 匹配的内容:IIS   
  20.   
  21. ⑤从左向右匹配字符串:  
  22. 匹配的位置:0 匹配的内容:http://192.168.0.1:2008    
  23. 匹配的组 1:http://192.168.0.1:2008    
  24. 匹配的组 2:http   
  25. 匹配的组 3:192.168.0.1   
  26. 匹配的组 4:2008   

 

 

posted on 2013-04-09 21:21  freeliver54  阅读(500)  评论(0编辑  收藏  举报

导航