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
(0)

相关推荐

  • c#匹配整数和小数的正则表达式

    匹配整数的一种表达式: Regex.IsMatch(inputerstr, "^([0-9]{1,})$") 其中Inputerstr是要匹配的字符串 这个表达式仅匹配整数,如果是整数形式返回true,否则为false 匹配小数格式的表达式: Regex.IsMatch(inputerstr, "^([0-9]{1,}[.][0-9]*)$") 其中Inputerstr是要匹配的字符串 这个表达式仅匹配数字中有小数点格式的数字,如果是带有小数点格式的纯数字,返回tr

  • C#正则表达式的递归匹配分析

    在C#程序设计中经常会遇到这样的需求,要求匹配出成对的小括号里的内容,但是一般正则表达式中的 ?R 的语法似乎在C#中不被支持, 经过一番查找与测试,终于找到以下一段描述 /(  应该是 \( 不是用 /转义而是用 \来转义 匹配嵌套的构造 微软公司已经包含了一个有趣的创新来匹配稳定的构造(历史上,这是正则表达式所做不到的).这并不容易掌握 - 尽管这节较短,但是注意,它非常的晦涩难懂. 从一个例子开始可能更简单一些,所以我用这段代码作为开始: Regex r = new Regex(@"/((

  • C#正则函数用法实例【匹配、替换、提取】

    本文实例讲述了C#正则函数用法.分享给大家供大家参考,具体如下: System.Text.RegularExpressions 命名空间包含一些类,这些类提供对 .NET Framework 正则表达式引擎的访问.该命名空间提供正则表达式功能,可以从运行在 Microsoft .NET Framework 内的任何平台或语言中使用该功能. 1 正则表达式的常见使用 ① 格式匹配 /// <summary> /// 邮箱格式验证 /// </summary> /// <retu

  • C#正则表达式匹配HTML中的图片路径,图片地址代码

    一般来说一个 HTML 文档有很多标签,比如"<html>"."<body>"."<table>"等,想把文档中的 img 标签提取出来并不是一件容易的事.由于 img 标签样式变化多端,使提取的时候用程序寻找并不容易.于是想要寻找它们就必须写一个非常健全的正则表达式,不然有可能会找得不全,或者找出来的不是正确的 img 标签.我们可以从 HTML 标签的格式去想应该怎么建这个正则表达式.首先要想一下 img

  • C#中正则表达式的3种匹配模式

    在C#中,我们一般使用Regex类来表示一个正则表达式.一般正则表达式引擎支持以下3种匹配模式:单行模式(Singleline).多行模式(Multiline)与忽略大小写(IgnoreCase). 1. 单行模式(Singleline) MSDN定义:更改点 (.) 的含义,使它与每一个字符匹配(而不是与除 \n 之外的每个字符匹配). 使用单行模式的典型场景是获取网页源码中的信息. 示例: 我们使用WebBrowser控件,从http://www.xxx.com/1.htm上获取了如下HTM

  • JS和C#实现的两个正则替换功能示例分析

    本文实例讲述了JS和C#实现的两个正则替换功能.分享给大家供大家参考,具体如下: 应用实例1: 待处理字符串:str="display=test name=mu display=temp" 要求:把display=后的值都改成localhost JS处理方法: str.replace(/display=\w*/g,"display=localhost"); C#处理方法: Regex reg=new Regex(@"display=\w*");

  • C#匹配中文字符串的4种正则表达式分享

    本文介绍在C#中使用匹配中文的正则表达式,包括纯中文.有中文.中文开头.中文结尾等几个正则表达式示例.在正则表达式中,中文可以通过Unicode编码来确定正则表达式范围. 在C#中,匹配中文的正则表达式用Unicode来表示时,范围是: [\u4e00-\u9fa5].所以,在此基础上,我们可以得到如下一些正则表达式. 1.匹配字符串全部是中文字符的正则表达式 复制代码 代码如下: "^[\u4e00-\u9fa5]+$" 说明:"^"表示字符串开头,"$

  • C#正则表达式匹配与替换字符串功能示例

    本文实例讲述了C#正则表达式匹配与替换字符串功能.分享给大家供大家参考,具体如下: 事例一:\w+=>[A-Za-z1-9_],\s+=>任何空白字符,()=>捕获 string text = @"public string testMatchObj string s string match "; string pat = @"(\w+)\s+(string)"; // Compile the regular expression. Regex

  • c#使用正则表达式匹配字符串验证URL示例

    在System.Text.RegularExpression命名空间里,有正则表达式方法. 复制代码 代码如下: using System.Collections.Generic; using System.Text;using System.Text.RegularExpressions; namespace RegexDemo{    class Program    {        static void Main(string[] args)        {            R

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

    使用Regex类需要引用命名空间:using System.Text.RegularExpressions; 一.利用Regex类实现验证 示例1:注释的代码所起的作用是相同的,不过一个是静态方法,一个是实例方法 var source = "刘备关羽张飞孙权何问起"; //Regex regex = new Regex("孙权"); //if (regex.IsMatch(source)) //{ // Console.WriteLine("字符串中包含有

  • 实例分析js和C#中使用正则表达式匹配a标签

    废话不多说,都在代码中,直接上 JS代码: 复制代码 代码如下: <html> <head> <script language="javascript">     var a='<P><A href=\'~abc/ccg/ab.jpg\' width="3">文字</A><A width="4" style="color:#ddd; font-weight:b

随机推荐