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常用匹配
在线测试:http://tool.hovertree.com/a/zz/
1 #region 身份证号码正则表达式 2 //何问起 3 4 Console.WriteLine("请输入一个身份证号码"); 5 string id = Console.ReadLine(); 6 bool b4 = Regex.IsMatch(id, @"^\d{15}|\d{18}$"); 7 bool b5 = Regex.IsMatch(id, @"^(\d{15}|\d{18})$"); 8 Console.WriteLine(b4); 9 Console.WriteLine(b5); 10 11 #endregion 12 13 #region 匹配电话号码 14 //hovertree 15 16 17 Console.WriteLine("请输入电话号码"); 18 string phone = Console.ReadLine(); 19 bool b = Regex.IsMatch(phone, @"^((\d{3,4}\-\d?{7,8})|(\d{5}))$"); 20 Console.WriteLine(b); 21 22 #endregion 23 24 #region 匹配email的regex 25 26 //hovertree 27 28 Console.WriteLine("请输入Email地址"); 29 string email = Console.ReadLine(); 30 bool bhvt = Regex.IsMatch(email, @"^\w+@\w+\.\w+$"); 31 Console.WriteLine(bhvt); 32 33 #endregion 34 35 #region 匹配ip地址的regex 36 //hovertree 37 38 Console.WriteLine("请输入一个IP地址"); 39 string ip = Console.ReadLine(); 40 bool bkly = Regex.IsMatch(ip, @"^\d{1,3}(\.\d{1,3}){3}$"); 41 Console.WriteLine(bkly); 42 43 #endregion 44 45 #region 匹配日期合法regex 46 //何问起 47 48 Console.WriteLine("请输入一个日期"); 49 string date = Console.ReadLine(); 50 bool bhovertree = Regex.IsMatch(date, @"^\d{4}\-\d{1,2}\-\d{1,2}$"); 51 Console.WriteLine(bhovertree); 52 53 #endregion 54 55 56 #region 匹配url地址的regex 57 //"http://hovertree.com" 58 //"http://keleyi.com/a/bjae/h1o76nuh.htm?id=3&name=aaa" 59 //"https://s.taobao.com/search?q=hover+tree&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20151204&ie=utf8" 60 //"ftp://127.0.0.1/myslider.txt" 61 62 //hovertree 63 64 Console.WriteLine("请输入url地址"); 65 string url = Console.ReadLine(); 66 bool bkeleyi = Regex.IsMatch(url, @"^[a-zA-Z]+://.+$"); 67 Console.WriteLine(bkeleyi); 68 69 #endregion
ASP.NET开源CMS http://www.cnblogs.com/sosoft/p/cms.html
分类:
.NET
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
2013-12-04 PHP循环语句基础介绍
2013-12-04 C#使用Graphics画圆写字
2013-12-04 新学C#的List<T>总结
2013-12-04 快速学习JavaScript面向对象编程