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

 更新时间:2015年12月06日 15:51:44   投稿:hebedich  
本文通过几个具体的实例,给大家详细介绍了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/

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

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

#endregion

相关文章

  • 基于Kubernetes实现前后端应用的金丝雀发布(两种方案)

    基于Kubernetes实现前后端应用的金丝雀发布(两种方案)

    这篇文章主要介绍了基于Kubernetes实现前后端应用的金丝雀发布,文中给大家提到了两种常用方案,通过示例代码给大家介绍的非常详细,需要的朋友可以参考下
    2021-12-12
  • C#调用dos窗口获取相关信息的方法

    C#调用dos窗口获取相关信息的方法

    这篇文章主要介绍了C#调用dos窗口获取相关信息的方法,涉及C#调用dos窗口及进程操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • 使用 C# 动态编译代码和执行的代码

    使用 C# 动态编译代码和执行的代码

    一个控制台例子, 演示了如何用 C# 动态的生成代码, 编译代码, 最后执行
    2011-05-05
  • C#请求http向网页发送接收数据的方法

    C#请求http向网页发送接收数据的方法

    这篇文章主要为大家详细介绍了C#请求http向网页发送数据、网页接收的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • C#实现异步的常用方式总结

    C#实现异步的常用方式总结

    这篇文章主要为大家详细介绍了C#实现异步的几个常用方式,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解一下
    2023-05-05
  • C# ManualResetEvent用法详解

    C# ManualResetEvent用法详解

    这篇文章主要介绍了C# ManualResetEvent用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • C#实现读取匿名对象属性值的方法示例总结

    C#实现读取匿名对象属性值的方法示例总结

    这篇文章主要介绍了C#实现读取匿名对象属性值的方法,结合实例形式总结分析了C#通过反射、转换等方法读取匿名对象属性值的相关操作技巧,需要的朋友可以参考下
    2020-03-03
  • 字符串替换Replace仅替换第一个字符串匹配项

    字符串替换Replace仅替换第一个字符串匹配项

    C#里面的String.Replace(string,string)方法替换的时候是替换所有的匹配项,我们需要只替换第一个匹配项,写一个方法来实现这个功能
    2013-12-12
  • C# 创建EXCEL图表并保存为图片的实例

    C# 创建EXCEL图表并保存为图片的实例

    下面小编就为大家分享一篇C# 创建EXCEL图表并保存为图片的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • 轻松学习C#的运算符

    轻松学习C#的运算符

    轻松学习C#的运算符,对C#的运算符感兴趣的朋友可以参考本篇文章,帮助大家更灵活的运用C#的运算符
    2015-11-11

最新评论