详解C#正则表达式Regex常用匹配
作者:柔城 发布时间:2022-07-08 08:04:45
标签:C#,正则表达式,Regex
使用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


猜你喜欢
- 今天一起学习下如何在Spring中进行异步编程。我们都知道,web服务器处理请求 request 的线程是从线程池中获取的,这也不难解释,因
- 在使用Android Studio开发的时候,如遇到多个项目引用同一个library的情况时,会遇到在每个项目中都要有一套library的代
- 前言但是在实际业务场景中,数据量迅速增长,一个库一个表已经满足不了我们的需求的时候,我们就会考虑分库分表的操作,在springboot中如何
- 在Android控件View的文字周围添加图标,供大家参考,具体内容如下在控件TextView文字周围放置图片(基于TextView的But
- 本文实例为大家分享了Android实现毛玻璃效果弹出菜单动画的具体代码,供大家参考,具体内容如下仿ios上屏幕下方向上滑出来的一个模糊菜单,
- 1. maven项目导入idea报ComponentLookupException异常1.1. 问题描述最近将IDEA 升级到 Intell
- 最近项目中用到的两种文件上传方式做一下总结:一. uploadify:uploadify控件的scripts和styles在这里:图片上传J
- mybatis 报错显示sql中有两个limit使用mybatis进行分页查询时,打印的查询sql中带有两个limit。经过审查:原因是由于
- 前言MVC模式是目前主流项目的标准开发模式,这种模式下框架的分层结构清晰,主要分为Controller,Service,Dao。分层的结构下
- WebView设置WebViewClient的方法shouldOverrideUrlLoading:在web页面里单击链接的时候,会自动调用
- 当setCanceledOnTouchOutside(true),点击阴影处,dialog dismiss时键盘不消失的问题。如图一开始觉得
- Java的动态绑定所谓的动态绑定就是指程执行期间(而不是在编译期间)判断所引用对象的实际类型,根据其实际的类型调用其相应的方法。java继承
- springmvc @RequestBody String类型参数通过如下配置: <bean id="mapp
- UGUI的滑动组件虽然表现上和NGUI的ScrollView一致,但是它更美好的是开放源码的,不了解原理的时候直接查源码就OK。在使用Scr
- MAC算法之消息摘要算法HmacMD5的实现MAC算法主要用于消息验证以下为算法实现:import javax.crypto.KeyGene
- 在安卓操作系统下对于 TextView 字体的支持非常有限,默认情况下 TextView 的 typeface 属性支持 "San
- 本文实例讲述了Java实现字符串解析为日期时间的方法。分享给大家供大家参考,具体如下:Java版本:1.8开始import java.tim
- 自定义过滤器获取不到session根本原因,多个自定义过滤器执行顺序问题问题action请求中request对象为ShiroHttpServ
- 为什么使用JUnit5JUnit4被广泛使用,但是许多场景下使用起来语法较为繁琐,JUnit5中支持lambda表达式,语法简单且代码不冗余
- <script>//验证身份证号方法var test=function(idcard){var Errors=new Array