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

 更新时间:2015年12月04日 09:00:41   作者:柔城  
这篇文章主要介绍了C#正则表达式Regex常用匹配,利用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

以上就是关于C#正则表达式Regex常用匹配的详细介绍,希望对大家的学习有所帮助。

相关文章

  • C#中yield用法使用说明

    C#中yield用法使用说明

    本文介绍了C#中yield的使用方法,yield 语句不能出现在匿名方法,其他相关内容就仔细阅读下文吧
    2015-10-10
  • C#实现简单订单管理程序

    C#实现简单订单管理程序

    这篇文章主要为大家详细介绍了C#实现简单订单管理程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • 一个可携带附加消息的增强消息框MessageBoxEx

    一个可携带附加消息的增强消息框MessageBoxEx

    一个可携带附加消息的增强消息框MessageBoxEx分享给大家,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • C#装箱和拆箱操作实例分析

    C#装箱和拆箱操作实例分析

    这篇文章主要介绍了C#装箱和拆箱操作,结合实例形式分析了C#中装箱与拆箱的概念、用法及相关注意事项,需要的朋友可以参考下
    2016-08-08
  • C#实现骑士飞行棋

    C#实现骑士飞行棋

    这篇文章主要为大家详细介绍了C#实现骑士飞行棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • c# 数据库的 sql 参数封装类的编写

    c# 数据库的 sql 参数封装类的编写

    c# 数据库的 sql 参数封装类的编写...
    2007-12-12
  • C#传值方式实现不同程序窗体间通信实例

    C#传值方式实现不同程序窗体间通信实例

    Form2构造函数中接收一个string类型参数,即Form1中选中行的文本,将Form2的TextBox控件的Text设置为该string,即完成了Form1向Form2的传值
    2013-12-12
  • C#中的LINQ to Objects详解(2)

    C#中的LINQ to Objects详解(2)

    本文详细讲解了C#中的LINQ to Objects,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • 基于Aforge摄像头调用简单实例

    基于Aforge摄像头调用简单实例

    这篇文章主要为大家详细介绍了基于Aforge摄像头调用的简单实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • C#委托delegate实例解析

    C#委托delegate实例解析

    这篇文章主要介绍了C#委托delegate实例解析,对C#的委托做了较为深入的剖析,很适合初学者学习,需要的朋友可以参考下
    2014-08-08

最新评论