C#正則表達(dá)式學(xué)習(xí)筆記
有一段時(shí)間,C#正則表達(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)不知所蹤了?,F(xiàn)在用到正則的時(shí)候也比較少,把以前的筆記等整理一下,以志不忘。
1.“@”符號符下兩ows表研究室的火熱,當(dāng)晨在“@”雖然并非C#正則表達(dá)式的“成員”,但是它經(jīng)常與C#正則表達(dá)式出雙入對。“@”表示,跟在它后面的字符串是個(gè)“逐字字符串”,不是很好理解,舉個(gè)例子,以下兩個(gè)聲明是等效的:string x="D:\\My Huang\\My Doc";string y = @"D:\My Huang\My Doc";事實(shí)上,如果按如下聲明,C#將會報(bào)錯(cuò),因?yàn)椤癨”在C#中用于實(shí)現(xiàn)轉(zhuǎn)義,如“\n”換行:string x = "D:\My Huang\My Doc";
2.基本的語法字符。
\d 0-9的數(shù)字\D \d的補(bǔ)集(以所以字符為全集,下同.,即所有非數(shù)字的字符\w 單詞字符,指大小寫字母、0-9的數(shù)字、下劃線\W \w的補(bǔ)集\s 空白字符,包括換行符\n、回車符\r、制表符\t、垂直制表符\v、換頁符\f \S \s的補(bǔ)集。 除換行符\n外的任意字符[…] 匹配[]內(nèi)所列出的所有字符[^…] 匹配非[]內(nèi)所列出的字符下面提供一些簡單的示例:
- 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.定位字符“定位字符”所代表的是一個(gè)虛的字符,它代表一個(gè)位置,你也可以直觀地認(rèn)為“定位字符”所代表的是某個(gè)字符與字符間的那個(gè)微小間隙。
^表示其后的字符必須位于字符串的開始處$ 表示其前面的字符必須位于字符串的結(jié)束處\b 匹配一個(gè)單詞的邊界\B 匹配一個(gè)非單詞的邊界另外,還包括:\A 前面的字符必須位于字符處的開始處,\z 前面的字符必須位于字符串的結(jié)束處,\Z 前面的字符必須位于字符串的結(jié)束處,或者位于換行符前下面提供一些簡單的示例:
- 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è)完整的單詞
【編輯推薦】