C#正則表達(dá)式Regex類的常用匹配
使用Regex類需要引用命名空間:using System.Text.RegularExpressions;
利用Regex類實(shí)現(xiàn)驗(yàn)證
示例1:注釋的代碼所起的作用是相同的,不過一個是靜態(tài)方法,一個是實(shí)例方法
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類進(jìn)行替換
示例1:簡單情況
var source = "123abc456ABC789";
// 靜態(tài)方法
//var newSource=Regex.Replace(source,"abc","|",RegexOptions.IgnoreCase);
// 實(shí)例方法
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:將匹配到的選項(xiàng)替換為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#正則表達(dá)式Regex常用匹配
在線測試:http://tool.hovertree.com/a/zz/
#region 身份證號碼正則表達(dá)式
//何問起
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
相關(guān)文章
基于Kubernetes實(shí)現(xiàn)前后端應(yīng)用的金絲雀發(fā)布(兩種方案)
這篇文章主要介紹了基于Kubernetes實(shí)現(xiàn)前后端應(yīng)用的金絲雀發(fā)布,文中給大家提到了兩種常用方案,通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
C#調(diào)用dos窗口獲取相關(guān)信息的方法
這篇文章主要介紹了C#調(diào)用dos窗口獲取相關(guān)信息的方法,涉及C#調(diào)用dos窗口及進(jìn)程操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
C#請求http向網(wǎng)頁發(fā)送接收數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了C#請求http向網(wǎng)頁發(fā)送數(shù)據(jù)、網(wǎng)頁接收的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
C#實(shí)現(xiàn)讀取匿名對象屬性值的方法示例總結(jié)
這篇文章主要介紹了C#實(shí)現(xiàn)讀取匿名對象屬性值的方法,結(jié)合實(shí)例形式總結(jié)分析了C#通過反射、轉(zhuǎn)換等方法讀取匿名對象屬性值的相關(guān)操作技巧,需要的朋友可以參考下2020-03-03
字符串替換Replace僅替換第一個字符串匹配項(xiàng)
C#里面的String.Replace(string,string)方法替換的時候是替換所有的匹配項(xiàng),我們需要只替換第一個匹配項(xiàng),寫一個方法來實(shí)現(xiàn)這個功能2013-12-12
C# 創(chuàng)建EXCEL圖表并保存為圖片的實(shí)例
下面小編就為大家分享一篇C# 創(chuàng)建EXCEL圖表并保存為圖片的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12

