• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#正则表达式Regex类的常用匹配

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

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了马桶c的个人空间,c站,欲情 c max,维生素c,奔驰c200等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

使用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();

</div>

示例2:使用带两个参数的构造函数,第二个参数指示忽略大小写,很常用

var source = "123abc345DEf";
Regex regex = new Regex("def",RegexOptions.IgnoreCase);
if (regex.IsMatch(source))
{
  Console.WriteLine("字符串中包含有敏感词:def!");
} 
Console.ReadLine();

</div>

使用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();

</div>

结果:

原字符串: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>";
}

</div>

输出:

原字符串: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

</div>

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#利用ReportViewer生成报表
  • C#基于正则去掉注释的方法示例
  • C#中new的用法及与override的区别分析
  • C#实现两个richtextbox控件滚动条同步滚动的简单方法
  • C# for循环的经典案例集锦
  • C#操作word的方法示例
  • C#使用WebClient登录网站并抓取登录后的网页信息实现方法
  • C# WinForm制作异形窗体与控件的方法
  • C#实现Excel表数据导入Sql Server数据库中的方法
  • C#使用NPOI上传excel

相关文章

  • 2017-05-28C# 开发圆角控件(窗体)的具体实现
  • 2017-05-28C#之IP地址和整数互转的小例子
  • 2017-05-28linq语法基础使用示例
  • 2017-05-28C# 去除首尾字符或字符串的方法
  • 2017-05-28C# 特殊的string类型详解
  • 2017-05-28C#将Json解析成DateTable的方法
  • 2017-05-28解析Silverlight调用WCF/Rest异常的解决方法
  • 2017-05-28c#使用正则表达式匹配字符串验证URL示例
  • 2017-05-28C#命名空间与java包的区别分析
  • 2017-05-28C#使用带like的sql语句时防sql注入的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#简单读取、改变文件的创建、修改及访问时间的方法
    • C/C++ 传递动态内存的深入理解
    • c# 正则指引--字符组
    • C#实现图片上传(PC端和APP)保存及 跨域上传说明
    • 如何使用C#从word文档中提取图片
    • C#将时间转成文件名使用方法
    • C# WinForm中禁止改变窗口大小的方法
    • c#固定长度的随机字符串例子
    • 讲解C#设计模式编程中享元模式的运用
    • C#中静态的深入理解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有