详解C#正则表达式Regex常用匹配

2019-12-30 11:31:44王振洲

 三、C#正则表达式Regex常用匹配


#region 身份证号码正则表达式
      //何问起
      
        Console.WriteLine("请输入一个身份证号码");
        string id = Console.ReadLine();
        bool b4 = Regex.IsMatch(id, @"^d{15}|d{18}$");
        bool b5 = Regex.IsMatch(id, @"^(d{15}|d{18})$");
        Console.WriteLine(b4);
        Console.WriteLine(b5);
      
      #endregion

      #region 匹配电话号码
      //hovertree
      

        Console.WriteLine("请输入电话号码");
        string phone = Console.ReadLine();
        bool b = Regex.IsMatch(phone, @"^((d{3,4}-d?{7,8})|(d{5}))$");
        Console.WriteLine(b);
      
      #endregion

      #region 匹配email的regex

      //hovertree
      
        Console.WriteLine("请输入Email地址");
        string email = Console.ReadLine();
        bool bhvt = Regex.IsMatch(email, @"^w+@w+.w+$");
        Console.WriteLine(bhvt);
      
      #endregion

      #region 匹配ip地址的regex
      //hovertree
      
        Console.WriteLine("请输入一个IP地址");
        string ip = Console.ReadLine();
        bool bkly = Regex.IsMatch(ip, @"^d{1,3}(.d{1,3}){3}$");
        Console.WriteLine(bkly);
      
      #endregion

      #region 匹配日期合法regex
      //何问起
      
        Console.WriteLine("请输入一个日期");
        string date = Console.ReadLine();
        bool bhovertree = Regex.IsMatch(date, @"^d{4}-d{1,2}-d{1,2}$");
        Console.WriteLine(bhovertree);
      
      #endregion


      #region 匹配url地址的regex
      //"http://www.easck.com/a/bjae/h1o76nuh.htm?id=3&name=aaa"
      //"https://www.easck.com/search?q=hover+tree&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20151204&ie=utf8"
      //"ftp://127.0.0.1/myslider.txt"

      //hovertree
      
        Console.WriteLine("请输入url地址");
        string url = Console.ReadLine();
        bool bkeleyi = Regex.IsMatch(url, @"^[a-zA-Z]+://.+$");
        Console.WriteLine(bkeleyi);
      
      #endregion

以上就是关于C#正则表达式Regex常用匹配的详细介绍,希望对大家的学习有所帮助。



注:相关教程知识阅读请移步到c#教程频道。