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

作者:柔城 时间:2022-07-08 08:04:45 

使用Regex类需要引用命名空间:using System.Text.RegularExpressions;

一、利用Regex类实现验证

示例1:注释的代码所起的作用是相同的,不过一个是静态方法,一个是实例方法


var source = "刘备关羽张飞孙权何问起";
//Regex regex = new Regex("孙权");
//if (regex.IsMatch(source))
//{
// Console.WriteLine("字符串中包含有敏感词:孙权!");
//}
if (Regex.IsMatch(source, "孙权"))
{
Console.WriteLine("字符串中包含有敏感词:孙权!");
}
Console.ReadLine();

示例2:使用带两个参数的构造函数,第二个参数指示忽略大小写,很常用


var source = "123abc345DEf";
Regex regex = new Regex("def",RegexOptions.IgnoreCase);
if (regex.IsMatch(source))
{
Console.WriteLine("字符串中包含有敏感词:def!");
}
Console.ReadLine();

二、使用Regex类进行替换
示例1:简单情况


var source = "123abc456ABC789";
// 静态方法
//var newSource=Regex.Replace(source,"abc","|",RegexOptions.IgnoreCase);
// 实例方法
Regex regex = new Regex("abc", RegexOptions.IgnoreCase);
var newSource = regex.Replace(source, "|");
Console.WriteLine("原字符串:"+source);
Console.WriteLine("替换后的字符串:" + newSource);
Console.ReadLine();

结果:

原字符串:123abc456ABC789

替换后的字符串:123|456|789

示例2:将匹配到的选项替换为html代码,我们使用了MatchEvaluator委托


var source = "123abc456ABCD789";
Regex regex = new Regex("[A-Z]{3}", RegexOptions.IgnoreCase);
var newSource = regex.Replace(source,new MatchEvaluator(OutPutMatch));
Console.WriteLine("原字符串:"+source);
Console.WriteLine("替换后的字符串:" + newSource);
Console.ReadLine();

//柔城

private static string OutPutMatch(Match match)
{
return "<b>" +match.Value+ "</b>";
}

输出:

原字符串:123abc456ABCD789

替换后的字符串:123<b>abc</b>456<b>ABC</b>D789

 三、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://hovertree.com"
     //"http://keleyi.com/a/bjae/h1o76nuh.htm?id=3&name=aaa"
     //"https://s.taobao.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
0
投稿

猜你喜欢

  • Jenkins+Maven+SVN自动化部署java项目

    2021-06-01 11:28:27
  • 可视化Swing中JTable控件绑定SQL数据源的两种方法深入解析

    2023-11-28 19:13:58
  • java导出csv格式文件的方法

    2022-07-24 20:26:36
  • Spring Boot产生环形注入的解决方案

    2023-11-08 20:14:04
  • 如何用java程序(JSch)运行远程linux主机上的shell脚本

    2023-11-24 12:35:58
  • 详解JAVA流程控制语句

    2023-11-05 02:27:09
  • java使用Socket类接收和发送数据

    2021-05-26 15:28:47
  • c#文件名/路径处理方法示例

    2021-11-28 21:02:40
  • JAVA中 Spring定时器的两种实现方式

    2022-10-11 02:33:45
  • Flutter图片缓存管理ImageCache原理分析

    2023-08-20 23:26:19
  • 浅谈java+内存分配及变量存储位置的区别

    2022-07-09 00:46:47
  • 详解Android数据存储—使用SQLite数据库

    2023-07-31 03:14:08
  • Mybatis generator如何自动生成代码

    2023-08-13 10:15:15
  • IDEA部署JavaWeb项目到Tomcat服务器的方法

    2023-11-02 23:21:22
  • Java求1+2!+3!+...+20!的和的代码

    2021-12-16 10:55:23
  • Android中TextView显示插入的图片实现方法

    2023-08-06 00:27:42
  • 一篇文章带你入门Java变量

    2021-09-21 07:46:02
  • 分享Java常用开发编辑器工具

    2023-11-06 07:35:37
  • Spring boot2X负载均衡和反向代理实现过程解析

    2023-02-06 04:18:53
  • SpringCloud远程服务调用三种方式及原理

    2023-10-16 07:21:19
  • asp之家 软件编程 m.aspxhome.com