C#中正则表达式的使用

    在项目的具体业务逻辑中,有时候可能遇到一些特殊的业务逻辑处理,可能用其它的各种方法最终可以达到想要的效果,可是费了九牛二虎之力发现用C#中自带的正则表达式来处理一下可能会更快的实现,下面一起来看一下C#内的正则表达式的几个简单的应用。

用使用C#中自带的正则表达式校验,要先应用一个命名空间后才可以使用

using System.Text.RegularExpressions;//正则表达式的命名空间

1.通过Regex类实现正则表达式的匹配
            string[] valus = {"111-222-333","111-22-333" };
            string parent = @"^\d{3}-\d{3}-\d{3}$";
            for (int i = 0; i < valus.Length; i++)
            {
                if(Regex.IsMatch(valus[i],parent))//匹配数组中的值是否符合要验证的正则
                {
                    Console.WriteLine(valus[i]+"格式正确");
                }
                else
                    Console.WriteLine(valus[i]+"格式错误");
            }

 

2.通过Regex.Match找到要匹配的项
        private static void RegexMatch()
        {
            var input = "this is a ABC ABC !";
            var pattern = @"\b(\w+)\W(\1)\b";// \b表示边界,\w+表示多个字符,\W表示空格(\1)表示一个空格
            Match mat = Regex.Match(input, pattern);
            while (mat.Success)
            {
                Console.WriteLine("{0} is found",mat.Groups[1].Value);
                mat = mat.NextMatch();//表示mat中的下一个匹配项
            }
        }

 

3.  将匹配上的文字进行替换 Regex.Replace(需要匹配的文字,匹配的模板,替换项)
        private static void RegexReplace()
        {
            string pattern = @"\b\d+\.\d{2}\b";//匹配一个含有数字加.加两位数字的数字
            string Relpacemen = "a$&";//$&表示要替换的项(104.13) a代表在前面所要加的东西
            string input = "Total cost: 104.13 ";
            Console.WriteLine(Regex.Replace(input,pattern,Relpacemen));
        }

 

4. 寻找匹配项进行分割开 Regex.Spilt
        private static void RegexSplit()
        {
          string input= "1.a 2.b 3.c 4.e 5.f";
          string pattern = @"\b\d{1,2}\.";//\s代表空格

          foreach (var item in Regex.Split(input,pattern))
          {
              if(!String.IsNullOrEmpty(item))
              {
                  Console.WriteLine(item);
              }
          }
        }

 

5.通过Regex.Matches(需要查找的字符) 找到匹配项在字符中的位置
        private static void Match()
        {
            MatchCollection mc;
            Regex r = new Regex("abc");
            mc = r.Matches("1234abc1234abc");
            foreach (Match mat in mc)
            {
                Console.WriteLine("{0} is found,position is {1}",mat.Value,mat.Index);
            }
        }

6.通过Regex.Match获取里面所有的Group
        private static void Group()
        {
            string input = "born: july 2, 1900";
            string pattern = @"\b(\w+)\s(\d{1,2}),\s(\d{4})\b";

            Match mat = Regex.Match(input, pattern);
            if(mat.Success)
            {
                for (int i = 0; i < mat.Groups.Count; i++)
                {
                    Console.WriteLine("Group {0} : {1}",i,mat.Groups[i].Value);
                }
            }
        }

 

所有代码如:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 using System.Text.RegularExpressions;//正则表达式的命名空间
  8 
  9 namespace 正则表达式
 10 {
 11     class Program
 12     {
 13         static void Main(string[] args)
 14         {
 15             //1.通过Regex类实现正则表达式的匹配
 16             string[] valus = { "111-222-333", "111-22-333" };
 17             string parent = @"^\d{3}-\d{3}-\d{3}$";
 18             for (int i = 0; i < valus.Length; i++)
 19             {
 20                 if (Regex.IsMatch(valus[i], parent))
 21                 {
 22                     Console.WriteLine(valus[i] + "格式正确");
 23                 }
 24                 else
 25                     Console.WriteLine(valus[i] + "格式错误");
 26             }
 27 
 28             RegexMatch();
 29             RegexReplace();
 30             RegexSplit();
 31             Match();
 32             Group();
 33 
 34         }
 35 
 36         /// <summary>
 37         /// 通过Regex.Match找到要匹配的项
 38         /// </summary>
 39         private static void RegexMatch()
 40         {
 41             var input = "this is a ABC ABC !";
 42             var pattern = @"\b(\w+)\W(\1)\b";//\b表示边界,\w+表示多个字符,\W表示空格(\1)表示一个空格 
 43             Match mat = Regex.Match(input, pattern);
 44             while (mat.Success)
 45             {
 46                 Console.WriteLine("{0} is found", mat.Groups[1].Value);
 47                 mat = mat.NextMatch();
 48             }
 49         }
 50 
 51         /// <summary>
 52         /// 将匹配上的文字进行替换 Regex.Replace(需要匹配的文字,匹配的模板,替换项)
 53         /// </summary>
 54         private static void RegexReplace()
 55         {
 56             string pattern = @"\b\d+\.\d{2}\b";//匹配一个含有数字加.加两位数字的数字
 57             string Relpacemen = "a$&";//$&表示要替换的项(104.13) a代表在前面所要加的东西
 58             string input = "Total cost: 104.13 ";
 59             Console.WriteLine(Regex.Replace(input, pattern, Relpacemen));
 60         }
 61 
 62         /// <summary>
 63         /// 寻找匹配项进行分割开 Regex.Spilt
 64         /// </summary>
 65         private static void RegexSplit()
 66         {
 67             string input = "1.a 2.b 3.c 4.e 5.f";
 68             string pattern = @"\b\d{1,2}\.";//\s代表空格
 69 
 70             foreach (var item in Regex.Split(input, pattern))
 71             {
 72                 if (!String.IsNullOrEmpty(item))
 73                 {
 74                     Console.WriteLine(item);
 75                 }
 76             }
 77         }
 78 
 79         /// <summary>
 80         /// 通过Regex.Matches(需要查找的字符) 找到匹配项在字符中的位置
 81         /// </summary>
 82         private static void Match()
 83         {
 84             MatchCollection mc;
 85             Regex r = new Regex("abc");
 86             mc = r.Matches("1234abc1234abc");
 87             foreach (Match mat in mc)
 88             {
 89                 Console.WriteLine("{0} is found,position is {1}", mat.Value, mat.Index);
 90             }
 91         }
 92 
 93         /// <summary>
 94         /// 通过Regex.Match获取里面所有的Group
 95         /// </summary>
 96         private static void Group()
 97         {
 98             string input = "born: july 2, 1900";
 99             string pattern = @"\b(\w+)\s(\d{1,2}),\s(\d{4})\b";
100 
101             Match mat = Regex.Match(input, pattern);
102             if (mat.Success)
103             {
104                 for (int i = 0; i < mat.Groups.Count; i++)
105                 {
106                     Console.WriteLine("Group {0} : {1}", i, mat.Groups[i].Value);
107                 }
108             }
109         }
110 
111 
112 
113     }
114 }
View Code

 

posted @ 2016-08-03 01:34  微笑代表淡定.Net  阅读(126)  评论(0)    收藏  举报