有一段時(shí)間,正則表達(dá)式學(xué)習(xí)很火熱很潮流,當(dāng)時(shí)在CSDN一天就能看到好幾個(gè)正則表達(dá)式的帖子,那段時(shí)間借助論壇以及Wrox Press出版的《C#字符串和正則表達(dá)式參考手冊》學(xué)習(xí)了一些基礎(chǔ)的知識,同時(shí)也為我在CSDN大概賺了1000分,今天想起來,去找《C#字符串和正則表達(dá)式參考手冊》時(shí),已經(jīng)不知所蹤了。現(xiàn)在用到正則的時(shí)候也比較少,把以前的筆記等整理一下,以志不忘。 Code
string i = "\n"; string m = "3"; Regex r = new Regex(@"\D"); //同Regex r = new Regex("\\D"); //r.IsMatch(i)結(jié)果:true //r.IsMatch(m)結(jié)果:false string i = "%"; string m = "3"; Regex r = new Regex("[a-z0-9]"); //匹配小寫字母或數(shù)字字符 //r.IsMatch(i)結(jié)果:false //r.IsMatch(m)結(jié)果:true (3)定位字符 Code
string i = "Live for nothing,die for something"; Regex r1 = new Regex("^Live for nothing,die for something$"); //r1.IsMatch(i) true Regex r2 = new Regex("^Live for nothing,die for some$"); //r2.IsMatch(i) false Regex r3 = new Regex("^Live for nothing,die for some"); //r3.IsMatch(i) true string i = @"Live for nothing, die for something";//多行 Regex r1 = new Regex("^Live for nothing,die for something$"); Console.WriteLine("r1 match count:" + r1.Matches(i).Count);//0 Regex r2 = new Regex("^Live for nothing,die for something$", RegexOptions.Multiline); Console.WriteLine("r2 match count:" + r2.Matches(i).Count);//0 Regex r3 = new Regex("^Live for nothing,\r\ndie for something$"); Console.WriteLine("r3 match count:" + r3.Matches(i).Count);//1 Regex r4 = new Regex("^Live for nothing,$"); Console.WriteLine("r4 match count:" + r4.Matches(i).Count);//0 Regex r5 = new Regex("^Live for nothing,$", RegexOptions.Multiline); Console.WriteLine("r5 match count:" + r5.Matches(i).Count);//0 Regex r6 = new Regex("^Live for nothing,\r\n$"); Console.WriteLine("r6 match count:" + r6.Matches(i).Count);//0 Regex r7 = new Regex("^Live for nothing,\r\n$", RegexOptions.Multiline); Console.WriteLine("r7 match count:" + r7.Matches(i).Count);//0 Regex r8 = new Regex("^Live for nothing,\r$"); Console.WriteLine("r8 match count:" + r8.Matches(i).Count);//0 Regex r9 = new Regex("^Live for nothing,\r$", RegexOptions.Multiline); Console.WriteLine("r9 match count:" + r9.Matches(i).Count);//1 Regex r10 = new Regex("^die for something$"); Console.WriteLine("r10 match count:" + r10.Matches(i).Count);//0 Regex r11 = new Regex("^die for something$", RegexOptions.Multiline); Console.WriteLine("r11 match count:" + r11.Matches(i).Count);//1 Regex r12 = new Regex("^"); Console.WriteLine("r12 match count:" + r12.Matches(i).Count);//1 Regex r13 = new Regex("$"); Console.WriteLine("r13 match count:" + r13.Matches(i).Count);//1 Regex r14 = new Regex("^", RegexOptions.Multiline); Console.WriteLine("r14 match count:" + r14.Matches(i).Count);//2 Regex r15 = new Regex("$", RegexOptions.Multiline); Console.WriteLine("r15 match count:" + r15.Matches(i).Count);//2 Regex r16 = new Regex("^Live for nothing,\r$\n^die for something$", RegexOptions.Multiline); Console.WriteLine("r16 match count:" + r16.Matches(i).Count);//1 //對于一個(gè)多行字符串,在設(shè)置了Multiline選項(xiàng)之后,^和$將出現(xiàn)多次匹配。 string i = "Live for nothing,die for something"; string m = "Live for nothing,die for some thing"; Regex r1 = new Regex(@"\bthing\b"); Console.WriteLine("r1 match count:" + r1.Matches(i).Count);//0 Regex r2 = new Regex(@"thing\b"); Console.WriteLine("r2 match count:" + r2.Matches(i).Count);//2 Regex r3 = new Regex(@"\bthing\b"); Console.WriteLine("r3 match count:" + r3.Matches(m).Count);//1 Regex r4 = new Regex(@"\bfor something\b"); Console.WriteLine("r4 match count:" + r4.Matches(i).Count);//1 //\b通常用于約束一個(gè)完整的單詞 (4)重復(fù)描述字符 Code
string x = "1024"; string y = "+1024"; string z = "1,024"; string a = "1"; string b="-1024"; string c = "10000"; Regex r = new Regex(@"^\+?[1-9],?\d{3}$"); Console.WriteLine("x match count:" + r.Matches(x).Count);//1 Console.WriteLine("y match count:" + r.Matches(y).Count);//1 Console.WriteLine("z match count:" + r.Matches(z).Count);//1 Console.WriteLine("a match count:" + r.Matches(a).Count);//0 Console.WriteLine("b match count:" + r.Matches(b).Count);//0 Console.WriteLine("c match count:" + r.Matches(c).Count);//0 //匹配1000到9999的整數(shù)。 (5)擇一匹配 Code
string x = "0"; string y = "0.23"; string z = "100"; string a = "100.01"; string b = "9.9"; string c = "99.9"; string d = "99."; string e = "00.1"; Regex r = new Regex(@"^\+?((100(.0+)*)|([1-9]?[0-9])(\.\d+)*)$"); Console.WriteLine("x match count:" + r.Matches(x).Count);//1 Console.WriteLine("y match count:" + r.Matches(y).Count);//1 Console.WriteLine("z match count:" + r.Matches(z).Count);//1 Console.WriteLine("a match count:" + r.Matches(a).Count);//0 Console.WriteLine("b match count:" + r.Matches(b).Count);//1 Console.WriteLine("c match count:" + r.Matches(c).Count);//1 Console.WriteLine("d match count:" + r.Matches(d).Count);//0 Console.WriteLine("e match count:" + r.Matches(e).Count);//0 //匹配0到100的數(shù)。最外層的括號內(nèi)包含兩部分“(100(.0+)*)”,“([1-9]?[0-9])(\.\d+)*”,這兩部分是“OR”的關(guān)系,即正則表達(dá)式引擎會(huì)先嘗試匹配100,如果失敗,則嘗試匹配后一個(gè)表達(dá)式(表示[0,100)范圍中的數(shù)字)。 (6)特殊字符的匹配 Code
string x = "\\"; Regex r1 = new Regex("^\\\\$"); Console.WriteLine("r1 match count:" + r1.Matches(x).Count);//1 Regex r2 = new Regex(@"^\\$"); Console.WriteLine("r2 match count:" + r2.Matches(x).Count);//1 Regex r3 = new Regex("^\\$"); Console.WriteLine("r3 match count:" + r3.Matches(x).Count);//0 //匹配“\” string x = "\""; Regex r1 = new Regex("^\"$"); Console.WriteLine("r1 match count:" + r1.Matches(x).Count);//1 Regex r2 = new Regex(@"^""$"); Console.WriteLine("r2 match count:" + r2.Matches(x).Count);//1 //匹配雙引號 (7)組與非捕獲組 Code
string x = "Live for nothing,die for something"; string y = "Live for nothing,die for somebody"; Regex r = new Regex(@"^Live ([a-z]{3}) no([a-z]{5}),die \1 some\2$"); Console.WriteLine("x match count:" + r.Matches(x).Count);//1 Console.WriteLine("y match count:" + r.Matches(y).Count);//0 //正則表達(dá)式引擎會(huì)記憶“()”中匹配到的內(nèi)容,作為一個(gè)“組”,并且可以通過索引的方式進(jìn)行引用。表達(dá)式中的“\1”,用于反向引用表達(dá)式中出現(xiàn)的第一個(gè)組,即粗體標(biāo)識的第一個(gè)括號內(nèi)容,“\2”則依此類推。 string x = "Live for nothing,die for something"; Regex r = new Regex(@"^Live for no([a-z]{5}),die for some\1$"); if (r.IsMatch(x)) { Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//輸出:thing } //獲取組中的內(nèi)容。注意,此處是Groups[1],因?yàn)镚roups[0]是整個(gè)匹配的字符串,即整個(gè)變量x的內(nèi)容。 string x = "Live for nothing,die for something"; Regex r = new Regex(@"^Live for no(?<g1>[a-z]{5}),die for some\1$"); if (r.IsMatch(x)) { Console.WriteLine("group1 value:" + r.Match(x).Groups["g1"].Value);//輸出:thing } //可根據(jù)組名進(jìn)行索引。使用以下格式為標(biāo)識一個(gè)組的名稱(?<groupname>…)。 string x = "Live for nothing nothing"; Regex r = new Regex(@"([a-z]+) \1"); if (r.IsMatch(x)) { x = r.Replace(x, "$1"); Console.WriteLine("var x:" + x);//輸出:Live for nothing } //刪除原字符串中重復(fù)出現(xiàn)的“nothing”。在表達(dá)式之外,使用“$1”來引用第一個(gè)組,下面則是通過組名來引用: string x = "Live for nothing nothing"; Regex r = new Regex(@"(?<g1>[a-z]+) \1"); if (r.IsMatch(x)) { x = r.Replace(x, "${g1}"); Console.WriteLine("var x:" + x);//輸出:Live for nothing } string x = "Live for nothing"; Regex r = new Regex(@"^Live for no(?:[a-z]{5})$"); if (r.IsMatch(x)) { Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//輸出:(空) } //在組前加上“?:”表示這是個(gè)“非捕獲組”,即引擎將不保存該組的內(nèi)容。 (8)貪婪與非貪婪 Code
string x = "Live for nothing,die for something"; Regex r1 = new Regex(@".*thing"); if (r1.IsMatch(x)) { Console.WriteLine("match:" + r1.Match(x).Value);//輸出:Live for nothing,die for something } Regex r2 = new Regex(@".*?thing"); if (r2.IsMatch(x)) { Console.WriteLine("match:" + r2.Match(x).Value);//輸出:Live for nothing } (9)回溯與非回溯 Code
string x = "Live for nothing,die for something"; Regex r1 = new Regex(@".*thing,"); if (r1.IsMatch(x)) { Console.WriteLine("match:" + r1.Match(x).Value);//輸出:Live for nothing, } Regex r2 = new Regex(@"(?>.*)thing,"); if (r2.IsMatch(x))//不匹配 { Console.WriteLine("match:" + r2.Match(x).Value); } //在r1中,“.*”由于其貪婪特性,將一直匹配到字符串的最后,隨后匹配“thing”,但在匹配“,”時(shí)失敗,此時(shí)引擎將回溯,并在“thing,”處匹配成功。 在r2中,由于強(qiáng)制非回溯,所以整個(gè)表達(dá)式匹配失敗。 (10)正向預(yù)搜索、反向預(yù)搜索 Code
string x = "1024 used 2048 free"; Regex r1 = new Regex(@"\d{4}(?= used)"); if (r1.Matches(x).Count==1) { Console.WriteLine("r1 match:" + r1.Match(x).Value);//輸出:1024 } Regex r2 = new Regex(@"\d{4}(?! used)"); if (r2.Matches(x).Count==1) { Console.WriteLine("r2 match:" + r2.Match(x).Value); //輸出:2048 } //r1中的正聲明表示必須保證在四位數(shù)字的后面必須緊跟著“ used”,r2中的負(fù)聲明表示四位數(shù)字之后不能跟有“ used”。 反向預(yù)搜索聲明格式:正聲明“(?<=)”,負(fù)聲明“(?<!)”,聲明本身不作為最終匹配結(jié)果的一部分,請看下面的示例: Code
string x = "used:1024 free:2048"; Regex r1 = new Regex(@"(?<=used:)\d{4}"); if (r1.Matches(x).Count==1) { Console.WriteLine("r1 match:" + r1.Match(x).Value);//輸出:1024 } Regex r2 = new Regex(@"(?<!used:)\d{4}"); if (r2.Matches(x).Count==1) { Console.WriteLine("r2 match:" + r2.Match(x).Value);//輸出:2048 } //r1中的反向正聲明表示在4位數(shù)字之前必須緊跟著“used:”,r2中的反向負(fù)聲明表示在4位數(shù)字之前必須緊跟著除“used:”之外的字符串。 (11)十六進(jìn)制字符范圍
Code
Regex r = new Regex(@"^\+?0*(?:100(\.0*)?|(\d{0,2}(?=\.\d)|\d{1,2}(?=($|\.$)))(\.\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)精確匹配有時(shí)候是困難的 |
|