C#正则表达式Regex类的常用匹配

2019-12-30 11:29:40于海丽

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

在线测试:http://www.easck.com/a/zz/


#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"

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

#endregion


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