詳解C#正則表達(dá)式Regex常用匹配
使用Regex類需要引用命名空間:using System.Text.RegularExpressions;
一、利用Regex類實(shí)現(xiàn)驗(yàn)證
示例1:注釋的代碼所起的作用是相同的,不過(guò)一個(gè)是靜態(tài)方法,一個(gè)是實(shí)例方法
var source = "劉備關(guān)羽張飛孫權(quán)何問(wè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:使用帶兩個(gè)參數(shù)的構(gòu)造函數(shù),第二個(gè)參數(shù)指示忽略大小寫(xiě),很常用
var source = "123abc345DEf"; Regex regex = new Regex("def",RegexOptions.IgnoreCase); if (regex.IsMatch(source)) { Console.WriteLine("字符串中包含有敏感詞:def!"); } Console.ReadLine();
二、使用Regex類進(jìn)行替換
示例1:簡(jiǎn)單情況
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 "" +match.Value+ ""; }
輸出:
原字符串:123abc456ABCD789
替換后的字符串:123abc456ABCD789
三、C#正則表達(dá)式Regex常用匹配
#region 身份證號(hào)碼正則表達(dá)式 //何問(wèn)起 Console.WriteLine("請(qǐng)輸入一個(gè)身份證號(hào)碼"); 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 匹配電話號(hào)碼 //hovertree Console.WriteLine("請(qǐng)輸入電話號(hào)碼"); 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("請(qǐng)輸入Email地址"); string email = Console.ReadLine(); bool bhvt = Regex.IsMatch(email, @"^\w+@\w+\.\w+$"); Console.WriteLine(bhvt); #endregion #region 匹配ip地址的regex //hovertree Console.WriteLine("請(qǐng)輸入一個(gè)IP地址"); string ip = Console.ReadLine(); bool bkly = Regex.IsMatch(ip, @"^\d{1,3}(\.\d{1,3}){3}$"); Console.WriteLine(bkly); #endregion #region 匹配日期合法regex //何問(wèn)起 Console.WriteLine("請(qǐng)輸入一個(gè)日期"); 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("請(qǐng)輸入url地址"); string url = Console.ReadLine(); bool bkeleyi = Regex.IsMatch(url, @"^[a-zA-Z]+://.+$"); Console.WriteLine(bkeleyi); #endregion
以上就是關(guān)于C#正則表達(dá)式Regex常用匹配的詳細(xì)介紹,希望對(duì)大家的學(xué)習(xí)有所幫助。
上一篇:C#創(chuàng)建數(shù)據(jù)庫(kù)及導(dǎo)入sql腳本的方法
欄 目:C#教程
下一篇:C#自定義DataGridViewColumn顯示TreeView
本文標(biāo)題:詳解C#正則表達(dá)式Regex常用匹配
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6788.html
您可能感興趣的文章
- 01-10C#中Socket通信用法實(shí)例詳解
- 01-10C#編程自學(xué)之運(yùn)算符和表達(dá)式
- 01-10C#裝箱和拆箱原理詳解
- 01-10C#類的多態(tài)性詳解
- 01-10C#創(chuàng)建不規(guī)則窗體的4種方式詳解
- 01-10C#中深度復(fù)制和淺度復(fù)制詳解
- 01-10C#正則表達(dá)式的6個(gè)簡(jiǎn)單例子
- 01-10C#中的正則表達(dá)式介紹
- 01-10C#使用正則表達(dá)式實(shí)現(xiàn)首字母轉(zhuǎn)大寫(xiě)的方法
- 01-10輕松學(xué)習(xí)C#的正則表達(dá)式


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?