日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
C#正則表達(dá)式備忘錄

(1)“@”符號(hào)

符下兩ows表研究室的火熱,當(dāng)晨在“@”雖然并非C#正則表達(dá)式的“成員”,但是它經(jīng)常與C#正則表達(dá)式出雙入對(duì)?!癅”表示,跟在它后面的字符串是個(gè)“逐字字符串”,不是很好理解,舉個(gè)例子,以下兩個(gè)聲明是等效的:

string x="D:\\My Huang\\My Doc";

string y = @"D:\My Huang\My Doc";

事實(shí)上,如果按如下聲明,C#將會(huì)報(bào)錯(cuò),因?yàn)椤癨”在C#中用于實(shí)現(xiàn)轉(zhuǎn)義,如“\n”換行:

string x = "D:\My Huang\My Doc";

(2)基本的語(yǔ)法字符。

\d   0-9的數(shù)字

\D   \d的補(bǔ)集(以所以字符為全集,下同),即所有非數(shù)字的字符

\w   單詞字符,指大小寫字母、0-9的數(shù)字、下劃線

\W   \w的補(bǔ)集

\s   空白字符,包括換行符\n、回車符\r、制表符\t、垂直制表符\v、換頁(yè)符\f

\S   \s的補(bǔ)集

.   除換行符\n外的任意字符

[…]   匹配[]內(nèi)所列出的所有字符

[^…]   匹配非[]內(nèi)所列出的字符

下面提供一些簡(jiǎn)單的示例:

復(fù)制內(nèi)容到剪貼板 程序代碼

 
 
  1. string i = "\n";
  2. string m = "3";
  3. Regex r = new Regex(@"\D");
  4. //同Regex r = new Regex("\\D");
  5. //r.IsMatch(i)結(jié)果:true
  6. //r.IsMatch(m)結(jié)果:false
  7. string i = "%";
  8. string m = "3";
  9. Regex r = new Regex("[a-z0-9]");
  10. //匹配小寫字母或數(shù)字字符
  11. //r.IsMatch(i)結(jié)果:false
  12. //r.IsMatch(m)結(jié)果:true

(3)定位字符

“定位字符”所代表的是一個(gè)虛的字符,它代表一個(gè)位置,你也可以直觀地認(rèn)為“定位字符”所代表的是某個(gè)字符與字符間的那個(gè)微小間隙。

^   表示其后的字符必須位于字符串的開始處

$   表示其前面的字符必須位于字符串的結(jié)束處

\b   匹配一個(gè)單詞的邊界

\B   匹配一個(gè)非單詞的邊界

另外,還包括:\A   前面的字符必須位于字符處的開始處,\z   前面的字符必須位于字符串的結(jié)束處,\Z   前面的字符必須位于字符串的結(jié)束處,或者位于換行符前

下面提供一些簡(jiǎn)單的示例:

復(fù)制內(nèi)容到剪貼板 程序代碼

 
 
  1. string i = "Live for nothing,die for something";
  2. Regex r1 = new Regex("^Live for nothing,die for something$");
  3. //r1.IsMatch(i) true
  4. Regex r2 = new Regex("^Live for nothing,die for some$");
  5. //r2.IsMatch(i) false
  6. Regex r3 = new Regex("^Live for nothing,die for some");
  7. //r3.IsMatch(i) true
  8. string i = @"Live for nothing,
  9. die for something";//多行
  10. Regex r1 = new Regex("^Live for nothing,die for something$");
  11. Console.WriteLine("r1 match count:" + r1.Matches(i).Count);//0
  12. Regex r2 = new Regex("^Live for nothing,die for something$", RegexOptions.Multiline);
  13. Console.WriteLine("r2 match count:" + r2.Matches(i).Count);//0
  14. Regex r3 = new Regex("^Live for nothing,\r\ndie for something$");
  15. Console.WriteLine("r3 match count:" + r3.Matches(i).Count);//1
  16. Regex r4 = new Regex("^Live for nothing,$");
  17. Console.WriteLine("r4 match count:" + r4.Matches(i).Count);//0
  18. Regex r5 = new Regex("^Live for nothing,$", RegexOptions.Multiline);
  19. Console.WriteLine("r5 match count:" + r5.Matches(i).Count);//0
  20. Regex r6 = new Regex("^Live for nothing,\r\n$");
  21. Console.WriteLine("r6 match count:" + r6.Matches(i).Count);//0
  22. Regex r7 = new Regex("^Live for nothing,\r\n$", RegexOptions.Multiline);
  23. Console.WriteLine("r7 match count:" + r7.Matches(i).Count);//0
  24. Regex r8 = new Regex("^Live for nothing,\r$");
  25. Console.WriteLine("r8 match count:" + r8.Matches(i).Count);//0
  26. Regex r9 = new Regex("^Live for nothing,\r$", RegexOptions.Multiline);
  27. Console.WriteLine("r9 match count:" + r9.Matches(i).Count);//1
  28. Regex r10 = new Regex("^die for something$");
  29. Console.WriteLine("r10 match count:" + r10.Matches(i).Count);//0
  30. Regex r11 = new Regex("^die for something$", RegexOptions.Multiline);
  31. Console.WriteLine("r11 match count:" + r11.Matches(i).Count);//1
  32. Regex r12 = new Regex("^");
  33. Console.WriteLine("r12 match count:" + r12.Matches(i).Count);//1
  34. Regex r13 = new Regex("$");
  35. Console.WriteLine("r13 match count:" + r13.Matches(i).Count);//1
  36. Regex r14 = new Regex("^", RegexOptions.Multiline);
  37. Console.WriteLine("r14 match count:" + r14.Matches(i).Count);//2
  38. Regex r15 = new Regex("$", RegexOptions.Multiline);
  39. Console.WriteLine("r15 match count:" + r15.Matches(i).Count);//2
  40. Regex r16 = new Regex("^Live for nothing,\r$\n^die for something$", RegexOptions.Multiline);
  41. Console.WriteLine("r16 match count:" + r16.Matches(i).Count);//1
  42. //對(duì)于一個(gè)多行字符串,在設(shè)置了Multiline選項(xiàng)之后,^和$將出現(xiàn)多次匹配。
  43. string i = "Live for nothing,die for something";
  44. string m = "Live for nothing,die for some thing";
  45. Regex r1 = new Regex(@"\bthing\b");
  46. Console.WriteLine("r1 match count:" + r1.Matches(i).Count);//0
  47. Regex r2 = new Regex(@"thing\b");
  48. Console.WriteLine("r2 match count:" + r2.Matches(i).Count);//2
  49. Regex r3 = new Regex(@"\bthing\b");
  50. Console.WriteLine("r3 match count:" + r3.Matches(m).Count);//1
  51. Regex r4 = new Regex(@"\bfor something\b");
  52. Console.WriteLine("r4 match count:" + r4.Matches(i).Count);//1
  53. //\b通常用于約束一個(gè)完整的單詞

(4)重復(fù)描述字符

“重復(fù)描述字符”是體現(xiàn)C#正則表達(dá)式“很好很強(qiáng)大”的地方之一:

{n}   匹配前面的字符n次

{n,}   匹配前面的字符n次或多于n次

{n,m}   匹配前面的字符n到m次

?   匹配前面的字符0或1次

+   匹配前面的字符1次或多于1次

*   匹配前面的字符0次或式于0次

以下提供一些簡(jiǎn)單的示例:

復(fù)制內(nèi)容到剪貼板 程序代碼

 
 
  1. string x = "1024";
  2. string y = "+1024";
  3. string z = "1,024";
  4. string a = "1";
  5. string b="-1024";
  6. string c = "10000";
  7. Regex r = new Regex(@"^\+?[1-9],?\d{3}$");
  8. Console.WriteLine("x match count:" + r.Matches(x).Count);//1
  9. Console.WriteLine("y match count:" + r.Matches(y).Count);//1
  10. Console.WriteLine("z match count:" + r.Matches(z).Count);//1
  11. Console.WriteLine("a match count:" + r.Matches(a).Count);//0
  12. Console.WriteLine("b match count:" + r.Matches(b).Count);//0
  13. Console.WriteLine("c match count:" + r.Matches(c).Count);//0
  14. //匹配1000到9999的整數(shù)。

(5)擇一匹配

C#正則表達(dá)式中的 (|) 符號(hào)似乎沒(méi)有一個(gè)專門的稱謂,姑且稱之為“擇一匹配”吧。事實(shí)上,像[a-z]也是一種擇一匹配,只不過(guò)它只能匹配單個(gè)字符,而(|)則提供了更大的范圍,(ab|xy)表示匹配ab或匹配xy。注意“|”與“()”在此是一個(gè)整體。下面提供一些簡(jiǎn)單的示例:

復(fù)制內(nèi)容到剪貼板 程序代碼

 
 
  1. string x = "0";
  2. string y = "0.23";
  3. string z = "100";
  4. string a = "100.01";
  5. string b = "9.9";
  6. string c = "99.9";
  7. string d = "99.";
  8. string e = "00.1";
  9. Regex r = new Regex(@"^\+?((100(.0+)*)|([1-9]?[0-9])(\.\d+)*)$");
  10. Console.WriteLine("x match count:" + r.Matches(x).Count);//1
  11. Console.WriteLine("y match count:" + r.Matches(y).Count);//1
  12. Console.WriteLine("z match count:" + r.Matches(z).Count);//1
  13. Console.WriteLine("a match count:" + r.Matches(a).Count);//0
  14. Console.WriteLine("b match count:" + r.Matches(b).Count);//1
  15. Console.WriteLine("c match count:" + r.Matches(c).Count);//1
  16. Console.WriteLine("d match count:" + r.Matches(d).Count);//0
  17. Console.WriteLine("e match count:" + r.Matches(e).Count);//0

//匹配0到100的數(shù)。最外層的括號(hào)內(nèi)包含兩部分“(100(.0+)*)”,“([1-9]?[0-9])(\.\d+)*”,這兩部分是“OR”的關(guān)系,即正則表達(dá)式引擎會(huì)先嘗試匹配100,如果失敗,則嘗試匹配后一個(gè)表達(dá)式(表示[0,100)范圍中的數(shù)字)。

(6)特殊字符的匹配

下面提供一些簡(jiǎn)單的示例:

復(fù)制內(nèi)容到剪貼板 程序代碼

 
 
  1. string x = "\\";
  2. Regex r1 = new Regex("^\\\\$");
  3. Console.WriteLine("r1 match count:" + r1.Matches(x).Count);//1
  4. Regex r2 = new Regex(@"^\\$");
  5. Console.WriteLine("r2 match count:" + r2.Matches(x).Count);//1
  6. Regex r3 = new Regex("^\\$");
  7. Console.WriteLine("r3 match count:" + r3.Matches(x).Count);//0
  8. //匹配“\”
  9. string x = "\"";
  10. Regex r1 = new Regex("^\"$");
  11. Console.WriteLine("r1 match count:" + r1.Matches(x).Count);//1
  12. Regex r2 = new Regex(@"^""$");
  13. Console.WriteLine("r2 match count:" + r2.Matches(x).Count);//1
  14. //匹配雙引號(hào)

(7)組與非捕獲組

以下提供一些簡(jiǎn)單的示例:

復(fù)制內(nèi)容到剪貼板 程序代碼

 
 
  1. string x = "Live for nothing,die for something";
  2. string y = "Live for nothing,die for somebody";
  3. Regex r = new Regex(@"^Live ([a-z]{3}) no([a-z]{5}),die \1 some\2$");
  4. Console.WriteLine("x match count:" + r.Matches(x).Count);//1
  5. Console.WriteLine("y match count:" + r.Matches(y).Count);//0

//正則表達(dá)式引擎會(huì)記憶“()”中匹配到的內(nèi)容,作為一個(gè)“組”,并且可以通過(guò)索引的方式進(jìn)行引用。表達(dá)式中的“\1”,用于反向引用表達(dá)式中出現(xiàn)的***個(gè)組,即粗體標(biāo)識(shí)的***個(gè)括號(hào)內(nèi)容,“\2”則依此類推。

 
 
  1. string x = "Live for nothing,die for something";
  2. Regex r = new Regex(@"^Live for no([a-z]{5}),die for some\1$");
  3. if (r.IsMatch(x))
  4. {
  5.      Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//輸出:thing
  6. }
  7. //獲取組中的內(nèi)容。注意,此處是Groups[1],因?yàn)镚roups[0]是整個(gè)匹配的字符串,即整個(gè)變量x的內(nèi)容。
  8. string x = "Live for nothing,die for something";
  9. Regex r = new Regex(@"^Live for no(?[a-z]{5}),die for some\1$");
  10. if (r.IsMatch(x))
  11. {
  12.      Console.WriteLine("group1 value:" + r.Match(x).Groups["g1"].Value);//輸出:thing
  13. }
  14. //可根據(jù)組名進(jìn)行索引。使用以下格式為標(biāo)識(shí)一個(gè)組的名稱(?…)。
  15. string x = "Live for nothing nothing";
  16. Regex r = new Regex(@"([a-z]+) \1");
  17. if (r.IsMatch(x))
  18. {
  19.      x = r.Replace(x, "$1");
  20.      Console.WriteLine("var x:" + x);//輸出:Live for nothing
  21. }
  22. //刪除原字符串中重復(fù)出現(xiàn)的“nothing”。在表達(dá)式之外,使用“$1”來(lái)引用***個(gè)組,下面則是通過(guò)組名來(lái)引用:
  23. string x = "Live for nothing nothing";
  24. Regex r = new Regex(@"(?[a-z]+) \1");
  25. if (r.IsMatch(x))
  26. {
  27.      x = r.Replace(x, "${g1}");
  28.      Console.WriteLine("var x:" + x);//輸出:Live for nothing
  29. }
  30. string x = "Live for nothing";
  31. Regex r = new Regex(@"^Live for no(?:[a-z]{5})$");
  32. if (r.IsMatch(x))
  33. {
  34.      Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//輸出:(空)
  35. }
  36. //在組前加上“?:”表示這是個(gè)“非捕獲組”,即引擎將不保存該組的內(nèi)容。

(8)貪婪與非貪婪

正則表達(dá)式的引擎是貪婪,只要模式允許,它將匹配盡可能多的字符。通過(guò)在“重復(fù)描述字符”(*,+)后面添加“?”,可以將匹配模式改成非貪婪。請(qǐng)看以下示例:

復(fù)制內(nèi)容到剪貼板 程序代碼

 
 
  1. string x = "Live for nothing,die for something";
  2. Regex r1 = new Regex(@".*thing");
  3. if (r1.IsMatch(x))
  4. {
  5.      Console.WriteLine("match:" + r1.Match(x).Value);//輸出:Live for nothing,die for something
  6. }
  7. Regex r2 = new Regex(@".*?thing");
  8. if (r2.IsMatch(x))
  9. {
  10.      Console.WriteLine("match:" + r2.Match(x).Value);//輸出:Live for nothing
  11. }

(9)回溯與非回溯

使用“(?>…)”方式進(jìn)行非回溯聲明。由于正則表達(dá)式引擎的貪婪特性,導(dǎo)致它在某些情況下,將進(jìn)行回溯以獲得匹配,請(qǐng)看下面的示例:

復(fù)制內(nèi)容到剪貼板 程序代碼

 
 
  1. string x = "Live for nothing,die for something";
  2. Regex r1 = new Regex(@".*thing,");
  3. if (r1.IsMatch(x))
  4. {
  5.      Console.WriteLine("match:" + r1.Match(x).Value);//輸出:Live for nothing,
  6. }
  7. Regex r2 = new Regex(@"(?>.*)thing,");
  8. if (r2.IsMatch(x))//不匹配
  9. {
  10.      Console.WriteLine("match:" + r2.Match(x).Value);
  11. }

//在r1中,“.*”由于其貪婪特性,將一直匹配到字符串的***,隨后匹配“thing”,但在匹配“,”時(shí)失敗,此時(shí)引擎將回溯,并在“thing,”處匹配成功。

在r2中,由于強(qiáng)制非回溯,所以整個(gè)表達(dá)式匹配失敗。

(10)正向預(yù)搜索、反向預(yù)搜索

正向預(yù)搜索聲明格式:正聲明 “(?=…)”,負(fù)聲明 “(?!...)” ,聲明本身不作為最終匹配結(jié)果的一部分,請(qǐng)看下面的示例:

復(fù)制內(nèi)容到剪貼板 程序代碼

 
 
  1. string x = "1024 used 2048 free";
  2. Regex r1 = new Regex(@"\d{4}(?= used)");
  3. if (r1.Matches(x).Count==1)
  4. {
  5.      Console.WriteLine("r1 match:" + r1.Match(x).Value);//輸出:1024
  6. }
  7. Regex r2 = new Regex(@"\d{4}(?! used)");
  8. if (r2.Matches(x).Count==1)
  9. {
  10.      Console.WriteLine("r2 match:" + r2.Match(x).Value); //輸出:2048
  11. }

//r1中的正聲明表示必須保證在四位數(shù)字的后面必須緊跟著“ used”,r2中的負(fù)聲明表示四位數(shù)字之后不能跟有“ used”。

反向預(yù)搜索聲明格式:正聲明“(?<=)”,負(fù)聲明“(?

 
 
  1. Code
  2. string x = "used:1024 free:2048";
  3. Regex r1 = new Regex(@"(?<=used:)\d{4}");
  4. if (r1.Matches(x).Count==1)
  5. {
  6.      Console.WriteLine("r1 match:" + r1.Match(x).Value);//輸出:1024
  7. }
  8. Regex r2 = new Regex(@"(?if (r2.Matches(x).Count==1)
  9. {
  10.      Console.WriteLine("r2 match:" + r2.Match(x).Value);//輸出:2048
  11. }

//r1中的反向正聲明表示在4位數(shù)字之前必須緊跟著“used:”,r2中的反向負(fù)聲明表示在4位數(shù)字之前必須緊跟著除“used:”之外的字符串。

(11)十六進(jìn)制字符范圍

正則表達(dá)式中,可以使用 "\xXX" 和 "\uXXXX" 表示一個(gè)字符("X" 表示一個(gè)十六進(jìn)制數(shù))形式字符范圍:

\xXX       編號(hào)在 0到255 范圍的字符,比如:空格可以使用 "\x20" 表示。

\uXXXX   任何字符可以使用 "\u" 再加上其編號(hào)的4位十六進(jìn)制數(shù)表示,比如:漢字可以使用“[\u4e00-\u9fa5]”表示。

(12)對(duì)[0,100]的比較完備的匹配

下面是一個(gè)比較綜合的示例,對(duì)于匹配[0,100],需要特殊考慮的地方包括

*00合法,00.合法,00.00合法,001.100合法

*空字符串不合法,僅小數(shù)點(diǎn)不合法,大于100不合法

*數(shù)值是可帶后綴的,如“1.07f”表示該值為一個(gè)float類型(未考慮)

復(fù)制內(nèi)容到剪貼板 程序代碼

 
 
  1. Regex r = new Regex(@"^\+?0*(?:100(\.0*)?|(\d{0,2}(?=\.\d)|\d{1,2}(?=($|\.$)))(\.\d*)?)$");
  2. string x = "";
  3. while (true)
  4. {
  5.      x = Console.ReadLine();
  6.      if (x != "exit")
  7.      {
  8.          if (r.IsMatch(x))
  9.          {
  10.              Console.WriteLine(x + " succeed!");
  11.          }
  12.          else
  13.          {
  14.              Console.WriteLine(x + " failed!");
  15.          }
  16.      }
  17.      else
  18.      {
  19.          break;
  20.      }
  21. }

(13)精確匹配有時(shí)候是困難的

有些需求要做到精確匹配比較困難,例如:日期、Url、Email地址等,其中一些你甚至需要研究一些專門的文檔寫出精確完備的表達(dá)式,對(duì)于這種情況,只能退而求其次,保證比較精確的匹配。例如對(duì)于日期,可以基于應(yīng)用系統(tǒng)的實(shí)際情況考慮一段較短的時(shí)間,或者對(duì)于像Email的匹配,可以只考慮最常見(jiàn)的形式。


當(dāng)前名稱:C#正則表達(dá)式備忘錄
當(dāng)前網(wǎng)址:http://www.dlmjj.cn/article/dhidspi.html