1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > c#中regex的命名空间_C#_详解C#正则表达式Regex常用匹配 使用Regex类需要引用命名空

c#中regex的命名空间_C#_详解C#正则表达式Regex常用匹配 使用Regex类需要引用命名空

时间:2020-07-08 07:40:03

相关推荐

c#中regex的命名空间_C#_详解C#正则表达式Regex常用匹配 使用Regex类需要引用命名空

详解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 "" +match.Value+ "";

}

输出:

原字符串:123abc456ABCD789

替换后的字符串:123abc456ABCD789

三、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

//""

//"/a/bjae/h1o76nuh.htm?id=3&name=aaa"

//"/search?q=hover+tree&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_1204&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常用匹配的详细介绍,希望对大家的学习有所帮助。相关阅读:

Android中AsyncTask详细介绍

PHP 处理TXT文件(打开/关闭/检查/读取)

简要讲解CSS中的类型选择器、ID选择器、类选择器

Java实现一个小说采集程序的简单实例

jQuery+jRange实现滑动选取数值范围特效

多个$(document).ready()的执行顺序实例分析

jquery中的查找parents与closest方法之间的区别

Linux中显示空闲内存空间的free命令的基本用法

php获取域名的google收录示例

JavaScript动态插入script的基本思路及实现函数

c#中Empty()和DefalutIfEmpty()用法分析

jquery编写Tab选项卡滚动导航切换特效

实例讲解Android App使用自带的SQLite数据库的基本方法

一款纯css3实现简单的checkbox复选框和radio单选框

c#中regex的命名空间_C#_详解C#正则表达式Regex常用匹配 使用Regex类需要引用命名空间 - phpStudy...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。