詳解C#正則表達式Regex常用匹配
使用Regex類需要引用命名空間:using System.Text.RegularExpressions;
一、利用Regex類實現(xiàn)驗證
示例1:注釋的代碼所起的作用是相同的,不過一個是靜態(tài)方法,一個是實例方法
var source = "劉備關(guān)羽張飛孫權(quán)何問起";
//Regex regex = new Regex("孫權(quán)");
//if (regex.IsMatch(source))
//{
// Console.WriteLine("字符串中包含有敏感詞:孫權(quán)!");
//}
if (Regex.IsMatch(source, "孫權(quán)"))
{
Console.WriteLine("字符串中包含有敏感詞:孫權(quán)!");
}
Console.ReadLine();
示例2:使用帶兩個參數(shù)的構(gòu)造函數(shù),第二個參數(shù)指示忽略大小寫,很常用
var source = "123abc345DEf";
Regex regex = new Regex("def",RegexOptions.IgnoreCase);
if (regex.IsMatch(source))
{
Console.WriteLine("字符串中包含有敏感詞:def!");
}
Console.ReadLine();
二、使用Regex類進行替換
示例1:簡單情況
var source = "123abc456ABC789";
// 靜態(tài)方法
//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();
結(jié)果:
原字符串: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
以上就是關(guān)于C#正則表達式Regex常用匹配的詳細介紹,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
C#下實現(xiàn)創(chuàng)建和刪除目錄的實例代碼
這篇文章主要介紹了C#下實現(xiàn)創(chuàng)建和刪除目錄的方法,功能非常實用,需要的朋友可以參考下2014-08-08
c#不使用windows api函數(shù)打開我的電腦和獲取電腦驅(qū)動器信息
這篇文章主要介紹了c#不使用windows api函數(shù)打開我的電腦和電腦驅(qū)動器信息的方法,大家參考使用2013-12-12
WPF實現(xiàn)XAML轉(zhuǎn)圖片的示例詳解
這篇文章主要為大家詳細介紹了如何利用WPF實現(xiàn)XAML轉(zhuǎn)圖片,文中的示例代碼講解詳細,對我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2022-11-11
在多線程中調(diào)用winform窗體控件的實現(xiàn)方法
這篇文章主要介紹了在多線程中調(diào)用winform窗體控件的實現(xiàn)方法,需要的朋友可以參考下2014-08-08
將ocx文件轉(zhuǎn)換成C#程序引用的DLL文件的辦法
將ocx文件轉(zhuǎn)換成C#程序引用的DLL文件的辦法,需要的朋友可以參考一下2013-03-03
C#利用iTextSharp組件給PDF文檔添加圖片/文字水印
這篇文章主要給大家介紹了關(guān)于如何C#利用iTextSharp組件給PDF文檔添加圖片/文字水印的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

