實例詳解C#正則表達式
有一段時間,正則表達式學(xué)習(xí)很火熱很潮流,當(dāng)時在腳本之間平臺一天就能看到好幾個正則表達式的帖子,那段時間借助論壇以及Wrox Press出版的《C#字符串和正則表達式參考手冊》學(xué)習(xí)了一些基礎(chǔ)的知識,同時也為我在CSDN大概賺了1000分,今天想起來,去找《C#字符串和正則表達式參考手冊》時,已經(jīng)不知所蹤了?,F(xiàn)在用到正則的時候也比較少,把以前的筆記等整理一下,以志不忘。
(1)“@”符號
符下兩ows表研究室的火熱,當(dāng)晨在“@”雖然并非C#正則表達式的“成員”,但是它經(jīng)常與C#正則表達式出雙入對?!癅”表示,跟在它后面的字符串是個“逐字字符串”,不是很好理解,舉個例子,以下兩個聲明是等效的:
string x="D://My Huang//My Doc"; string y = @"D:/My Huang/My Doc";
事實上,如果按如下聲明,C#將會報錯,因為“/”在C#中用于實現(xiàn)轉(zhuǎn)義,如“/n”換行:
string x = "D:/My Huang/My Doc";
(2)基本的語法字符。
/d 0-9的數(shù)字
/D /d的補集(以所以字符為全集,下同),即所有非數(shù)字的字符
/w 單詞字符,指大小寫字母、0-9的數(shù)字、下劃線
/W /w的補集
/s 空白字符,包括換行符/n、回車符/r、制表符/t、垂直制表符/v、換頁符/f
/S /s的補集
. 除換行符/n外的任意字符
[…] 匹配[]內(nèi)所列出的所有字符
[^…] 匹配非[]內(nèi)所列出的字符
下面提供一些簡單的示例:
string i = "/n"; string m = ""; Regex r = new Regex(@"/D"); //同Regex r = new Regex("http://D"); //r.IsMatch(i)結(jié)果:true //r.IsMatch(m)結(jié)果:false string i = "%"; string m = ""; Regex r = new Regex("[a-z-]"); //匹配小寫字母或數(shù)字字符 //r.IsMatch(i)結(jié)果:false //r.IsMatch(m)結(jié)果:true
(3)定位字符
“定位字符”所代表的是一個虛的字符,它代表一個位置,你也可以直觀地認為“定位字符”所代表的是某個字符與字符間的那個微小間隙。
^ 表示其后的字符必須位于字符串的開始處
$ 表示其前面的字符必須位于字符串的結(jié)束處
/b 匹配一個單詞的邊界
/B 匹配一個非單詞的邊界
另外,還包括:/A 前面的字符必須位于字符處的開始處,/z 前面的字符必須位于字符串的結(jié)束處,/Z 前面的字符必須位于字符串的結(jié)束處,或者位于換行符前
下面提供一些簡單的示例:
string i = "Live for nothing,die for something"; Regex r = new Regex("^Live for nothing,die for something$"); //r.IsMatch(i) true Regex r = new Regex("^Live for nothing,die for some$"); //r.IsMatch(i) false Regex r = new Regex("^Live for nothing,die for some"); //r.IsMatch(i) true string i = @"Live for nothing, die for something";//多行 Regex r = new Regex("^Live for nothing,die for something$"); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^Live for nothing,die for something$", RegexOptions.Multiline); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^Live for nothing,/r/ndie for something$"); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^Live for nothing,$"); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^Live for nothing,$", RegexOptions.Multiline); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^Live for nothing,/r/n$"); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^Live for nothing,/r/n$", RegexOptions.Multiline); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^Live for nothing,/r$"); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^Live for nothing,/r$", RegexOptions.Multiline); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^die for something$"); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^die for something$", RegexOptions.Multiline); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^"); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("$"); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^", RegexOptions.Multiline); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("$", RegexOptions.Multiline); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex("^Live for nothing,/r$/n^die for something$", RegexOptions.Multiline); Console.WriteLine("r match count:" + r.Matches(i).Count);// //對于一個多行字符串,在設(shè)置了Multiline選項之后,^和$將出現(xiàn)多次匹配。 string i = "Live for nothing,die for something"; string m = "Live for nothing,die for some thing"; Regex r = new Regex(@"/bthing/b"); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex(@"thing/b"); Console.WriteLine("r match count:" + r.Matches(i).Count);// Regex r = new Regex(@"/bthing/b"); Console.WriteLine("r match count:" + r.Matches(m).Count);// Regex r = new Regex(@"/bfor something/b"); Console.WriteLine("r match count:" + r.Matches(i).Count);// ///b通常用于約束一個完整的單詞
(4)重復(fù)描述字符
“重復(fù)描述字符”是體現(xiàn)C#正則表達式“很好很強大”的地方之一:
{n} 匹配前面的字符n次
{n,} 匹配前面的字符n次或多于n次
{n,m} 匹配前面的字符n到m次
? 匹配前面的字符0或1次
+ 匹配前面的字符1次或多于1次
* 匹配前面的字符0次或式于0次
以下提供一些簡單的示例:
string x = ""; string y = "+"; string z = ","; string a = ""; string b="-"; string c = ""; Regex r = new Regex(@"^/+?[-],?/d{}$"); Console.WriteLine("x match count:" + r.Matches(x).Count);// Console.WriteLine("y match count:" + r.Matches(y).Count);// Console.WriteLine("z match count:" + r.Matches(z).Count);// Console.WriteLine("a match count:" + r.Matches(a).Count);// Console.WriteLine("b match count:" + r.Matches(b).Count);// Console.WriteLine("c match count:" + r.Matches(c).Count);// //匹配到的整數(shù)。 //http://www.cnblogs.com/sosoft/
(5)擇一匹配
C#正則表達式中的 (|) 符號似乎沒有一個專門的稱謂,姑且稱之為“擇一匹配”吧。事實上,像[a-z]也是一種擇一匹配,只不過它只能匹配單個字符,而(|)則提供了更大的范圍,(ab|xy)表示匹配ab或匹配xy。注意“|”與“()”在此是一個整體。下面提供一些簡單的示例:
string x = ""; string y = "."; string z = ""; string a = "."; string b = "."; string c = "."; string d = "."; string e = "."; Regex r = new Regex(@"^/+?(((.+)*)|([-]?[-])(/./d+)*)$"); Console.WriteLine("x match count:" + r.Matches(x).Count);// Console.WriteLine("y match count:" + r.Matches(y).Count);// Console.WriteLine("z match count:" + r.Matches(z).Count);// Console.WriteLine("a match count:" + r.Matches(a).Count);// Console.WriteLine("b match count:" + r.Matches(b).Count);// Console.WriteLine("c match count:" + r.Matches(c).Count);// Console.WriteLine("d match count:" + r.Matches(d).Count);// Console.WriteLine("e match count:" + r.Matches(e).Count);// //匹配到的數(shù)
最外層的括號內(nèi)包含兩部分“(100(.0+)*)”,“([1-9]?[0-9])(/./d+)*”,這兩部分是“OR”的關(guān)系,即正則表達式引擎會先嘗試匹配100,如果失敗,則嘗試匹配后一個表達式(表示[0,100)范圍中的數(shù)字)。
(6)特殊字符的匹配
下面提供一些簡單的示例:
(7)組與非捕獲組
以下提供一些簡單的示例:
string x = "Live for nothing,die for something"; string y = "Live for nothing,die for somebody"; Regex r = new Regex(@"^Live ([a-z]{}) no([a-z]{}),die / some/$"); Console.WriteLine("x match count:" + r.Matches(x).Count);// Console.WriteLine("y match count:" + r.Matches(y).Count);// //正則表達式引擎會記憶“()”中匹配到的內(nèi)容,作為一個“組”,并且可以通過索引的方式進行引用
表達式中的“/”,用于反向引用表達式中出現(xiàn)的第一個組,即粗體標(biāo)識的第一個括號內(nèi)容,“/”則依此類推。
string x = "Live for nothing,die for something"; Regex r = new Regex(@"^Live for no([a-z]{}),die for some/$"); if (r.IsMatch(x)) { Console.WriteLine("group value:" + r.Match(x).Groups[].Value);//輸出:thing } //獲取組中的內(nèi)容。注意,此處是Groups[],因為Groups[]是整個匹配的字符串,即整個變量x的內(nèi)容。 // http://www.cnblogs.com/sosoft/ string x = "Live for nothing,die for something"; Regex r = new Regex(@"^Live for no(?<g>[a-z]{}),die for some/$"); if (r.IsMatch(x)) { Console.WriteLine("group value:" + r.Match(x).Groups["g"].Value);//輸出:thing } //可根據(jù)組名進行索引。使用以下格式為標(biāo)識一個組的名稱(?<groupname>…)。 string x = "Live for nothing nothing"; Regex r = new Regex(@"([a-z]+) /"); if (r.IsMatch(x)) { x = r.Replace(x, "$"); Console.WriteLine("var x:" + x);//輸出:Live for nothing } //刪除原字符串中重復(fù)出現(xiàn)的“nothing”。在表達式之外,使用“$”來引用第一個組,下面則是通過組名來引用: string x = "Live for nothing nothing"; Regex r = new Regex(@"(?<g>[a-z]+) /"); if (r.IsMatch(x)) { x = r.Replace(x, "${g}"); Console.WriteLine("var x:" + x);//輸出:Live for nothing } string x = "Live for nothing"; Regex r = new Regex(@"^Live for no(?:[a-z]{})$"); if (r.IsMatch(x)) { Console.WriteLine("group value:" + r.Match(x).Groups[].Value);//輸出:(空) } //在組前加上“?:”表示這是個“非捕獲組”,即引擎將不保存該組的內(nèi)容
(8)貪婪與非貪婪
正則表達式的引擎是貪婪,只要模式允許,它將匹配盡可能多的字符。通過在“重復(fù)描述字符”(*,+)后面添加“?”,可以將匹配模式改成非貪婪。請看以下示例:
string x = "Live for nothing,die for something"; Regex r = new Regex(@".*thing"); if (r.IsMatch(x)) { Console.WriteLine("match:" + r.Match(x).Value);//輸出:Live for nothing,die for something } Regex r = new Regex(@".*?thing"); if (r.IsMatch(x)) { Console.WriteLine("match:" + r.Match(x).Value);//輸出:Live for nothing }
(9)回溯與非回溯
使用“(?>…)”方式進行非回溯聲明。由于正則表達式引擎的貪婪特性,導(dǎo)致它在某些情況下,將進行回溯以獲得匹配,請看下面的示例:
string x = "Live for nothing,die for something"; Regex r = new Regex(@".*thing,"); if (r.IsMatch(x)) { Console.WriteLine("match:" + r.Match(x).Value);//輸出:Live for nothing, } Regex r = new Regex(@"(?>.*)thing,"); if (r.IsMatch(x))//不匹配 { Console.WriteLine("match:" + r.Match(x).Value); } //在r中,“.*”由于其貪婪特性,將一直匹配到字符串的最后,隨后匹配“thing”,但在匹配“,”時失敗,此時引擎將回溯,并在“thing,”處匹配成功
//在r中,由于強制非回溯,所以整個表達式匹配失敗。
(10)正向預(yù)搜索、反向預(yù)搜索
正向預(yù)搜索聲明格式:正聲明 “(?=…)”,負聲明 “(?!...)” ,聲明本身不作為最終匹配結(jié)果的一部分,請看下面的示例:
string x = " used free"; Regex r = new Regex(@"/d{}(?= used)"); if (r.Matches(x).Count==) { Console.WriteLine("r match:" + r.Match(x).Value);//輸出: } Regex r = new Regex(@"/d{}(?! used)"); if (r.Matches(x).Count==) { Console.WriteLine("r match:" + r.Match(x).Value); //輸出: } //r中的正聲明表示必須保證在四位數(shù)字的后面必須緊跟著“ used”,r2中的負聲明表示四位數(shù)字之后不能跟有“ used”
反向預(yù)搜索聲明格式:正聲明“(?<=)”,負聲明“(?<!)”,聲明本身不作為最終匹配結(jié)果的一部分,請看下面的示例:
string x = "used: free:"; Regex r = new Regex(@"(?<=used:)/d{}"); if (r.Matches(x).Count==) { Console.WriteLine("r match:" + r.Match(x).Value);//輸出: } Regex r = new Regex(@"(?<!used:)/d{}"); if (r.Matches(x).Count==) { Console.WriteLine("r match:" + r.Match(x).Value);//輸出: } //r中的反向正聲明表示在4位數(shù)字之前必須緊跟著“used:”,r2中的反向負聲明表示在4位數(shù)字之前必須緊跟著除“used:”之外的字符串
(11)十六進制字符范圍
正則表達式中,可以使用 "/xXX" 和 "/uXXXX" 表示一個字符("X" 表示一個十六進制數(shù))形式字符范圍:
/xXX 編號在 0到255 范圍的字符,比如:空格可以使用 "/x20" 表示。
/uXXXX 任何字符可以使用 "/u" 再加上其編號的4位十六進制數(shù)表示,比如:漢字可以使用“[/u4e00-/u9fa5]”表示。
(12)對[0,100]的比較完備的匹配
下面是一個比較綜合的示例,對于匹配[0,100],需要特殊考慮的地方包括
*00合法,00.合法,00.00合法,001.100合法
*空字符串不合法,僅小數(shù)點不合法,大于100不合法
*數(shù)值是可帶后綴的,如“1.07f”表示該值為一個float類型(未考慮)
Regex r = new Regex(@"^/+?*(?:(/.*)?|(/d{,}(?=/./d)|/d{,}(?=($|/.$)))(/./d*)?)$"); string x = ""; while (true) { x = Console.ReadLine(); if (x != "exit") { if (r.IsMatch(x)) { Console.WriteLine(x + " succeed!"); } else { Console.WriteLine(x + " failed!"); } } else { break; } }
(13)精確匹配有時候是困難的
有些需求要做到精確匹配比較困難,例如:日期、Url、Email地址等,其中一些你甚至需要研究一些專門的文檔寫出精確完備的表達式,對于這種情況,只能退而求其次,保證比較精確的匹配。例如對于日期,可以基于應(yīng)用系統(tǒng)的實際情況考慮一段較短的時間,或者對于像Email的匹配,可以只考慮最常見的形式。
以上內(nèi)容是小編給大家介紹的C#正則表達式,希望大家喜歡。同時感謝大家一直以來對我們網(wǎng)站的支持,祝大家元旦快樂。
上一篇:深入解析C#編程中struct所定義的結(jié)構(gòu)
欄 目:C#教程
下一篇:Dynamic和Var的區(qū)別及dynamic使用詳解
本文標(biāo)題:實例詳解C#正則表達式
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6754.html
您可能感興趣的文章
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#實現(xiàn)多線程下載文件的方法
- 01-10C#自定義簽名章實現(xiàn)方法
- 01-10C#文件斷點續(xù)傳實現(xiàn)方法
- 01-10C#實現(xiàn)簡單的Login窗口實例
- 01-10C#自定義事件監(jiān)聽實現(xiàn)方法
- 01-10C#實現(xiàn)寫系統(tǒng)日志的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實例總結(jié)
- 01-11ajax實現(xiàn)頁面的局部加載