正則表達(dá)式,主要是用符號(hào)描述了一類特定的文本(模式)。而正則表達(dá)式引擎則負(fù)責(zé)在給定的字符串中,查找到這一特定的文本。
本文主要是列出常用的正則表達(dá)式符號(hào),加以歸類說明。本文僅僅是快速理解了正則表達(dá)式相關(guān)元字符,作一個(gè)備忘,供以后理解更復(fù)雜表達(dá)式的參考,以后關(guān)于正則表達(dá)式的相關(guān)內(nèi)容會(huì)持續(xù)更新本文。示例語言用C#
概述
普通字符
字符集合
速記的字符集合
指定重復(fù)次數(shù)的字符
匹配位置字符
分支替換字符
匹配特殊字符
組,反向引用,非捕獲組
貪婪與非貪婪
回溯與非回溯
正向預(yù)搜索、反向預(yù)搜索
最后
最簡單的一種文本描述,就是直接給出要匹配內(nèi)容。 如要在”Generic specialization, the decorator pattern, chains of responsibilities, and extensible software.” 找到pattern,那么正則式就直接是”heels”即可

string input = "Generic specialization, the decorator pattern, chains of responsibilities, and extensible software."; Regex reg = new Regex("pattern", RegexOptions.IgnoreCase); Console.WriteLine(reg.Matches(input).Count); //output 1View Code將字符放在中括號(hào)中,即為字符集合。通過字符集合告訴正則式引擎從字符集合中的字符,僅匹配出一個(gè)字符。
| 字符 | 匹配的字符 | 示例 |
| [...] | 匹配括號(hào)中的任一字符 | [abc]可以匹配單個(gè)字符a,b或c,但不能匹配其它字符 |
| [^...] | 匹配非括號(hào)中的任一字符 | [^abc]可以匹配任一個(gè)除a,b,c的一個(gè)字符,如d,e,f |
比如單詞灰色gray(英)和grey(美),在一段文本中匹配出gray或grey,那么通過正則式gr[ae]y 就可以了;又比如要在一段文本中找到me和my,正則式是m[ey]
我們還可以在字符集合中使用連字號(hào) – 來表示一個(gè)范圍,比如 [0-9] 表示匹配一個(gè)0到9數(shù)字;[a-zA-Z] 表示匹配英文字母;[0-9 a-zA-Z]表示匹配一個(gè)0到9的數(shù)字或英文字母

string input = "The color of shirt is gray and color of shoes is grey too."; Regex reg = new Regex("gr[ae]y", RegexOptions.IgnoreCase); Console.WriteLine(reg.Matches(input).Count); //output 2 var matchs = reg.Matches(input); foreach (Match match in matchs) { Console.WriteLine(match.Value);//output gray grey }View Code我們常常要匹配一個(gè)數(shù)字,一個(gè)字母,一個(gè)空白符,雖然可以用普通的字符類來表示,但不夠方便,所以正則式提示了一些常用的字符集合的速記符
| 字符 | 匹配的字符 | 示例 |
| /d | 從0到9的任何一個(gè)數(shù)字 | /d/d 可以匹配72,但不能匹配me或7a |
| /D | 非數(shù)字符 | /D/D 可以匹配me,但不能匹配7a或72 |
| /w | 任一個(gè)單詞字符,如A-Z, a-z, 0-9和下劃線字符 | /w/w/w/w可以匹配aB_2,但不能匹配ab_@ |
| /W | 非單詞字符 | /W 可以匹配@,但不能匹配a |
| /s | 任一空白字符,包括了制表符,換行符,回車符,換頁符和垂直制表符 | 匹配所有傳統(tǒng)的空白字符 |
| /S | 任一非空白字符 | /S 可以匹配任一非空白字符,如~!@#& |
| . | 任一字符 | 匹配任一字符,換行符除外 |

string input = "1024 hello world&%"; Regex reg1 = new Regex(@"/d/d/d/d"); if (reg1.IsMatch(input)) { Console.WriteLine(reg1.Match(input).Value);//output 1024 } Regex reg2 = new Regex(@"/W/W"); if (reg2.IsMatch(input)) { Console.WriteLine(reg2.Match(input).Value);//output &% }View Code指定重復(fù)匹配前面的字符多少次:匹配重復(fù)的次數(shù),不匹配內(nèi)容。比如說,要在一系列電話號(hào)碼中找到以158開始的11位手機(jī)號(hào),如果我們沒有學(xué)過下面的內(nèi)容,正則表達(dá)式為158/d/d/d/d/d/d/d/d;但如果我們學(xué)習(xí)了下面的知識(shí),則正則表達(dá)式為158/d{8}
| 字符 | 匹配的字符 | 示例 |
| {n} | 匹配前面字符n次 | x{2},可以匹配xx,但不能匹配xxx |
| {n,} | 匹配前面字符n次或更多 | x{2,}可以2個(gè)或更多的x,比如可以匹配xx,xxx,xxxx,xxxxxx |
| {n,m} | 匹配前面字符最少n次,最多m次。如果n為0,則可以不指定 | x{2,4},匹配了xx,xxx,xxxx,但不能匹配x,xxxxx |
| ? | 匹配前面的字符0次或1次,相當(dāng)于{0,1} | x? 匹配x或空 |
| + | 匹配前面的字符1次或多次, 相當(dāng)于{1,} | x+ 匹配x,xx,或xxx |
| * | 匹配前面的字符0次或多次 | x* 匹配0個(gè)或多個(gè)x |

string input = "my phone number is 15861327445, please call me sometime."; Regex reg1 = new Regex(@"158/d{8}");//匹配以158為開頭的11位手機(jī)號(hào) if (reg1.IsMatch(input)) { Console.WriteLine(reg1.Match(input).Value);//output 15861327445 } string input2 = "November is the 11 month of the year, you can use Nov for short."; Regex reg2 = new Regex(@"Nov(ember)?");//匹配Nov 或者November var matchs = reg2.Matches(input2); foreach (Match match in matchs) { Console.WriteLine(match.Value);//output November Nov } string input3 = "1000, 100, 2003, 9999,10000"; Regex reg3 = new Regex(@"/b[1-9]/d{3}/b");//匹配1000到9999的數(shù)字 var matchs3 = reg3.Matches(input3); foreach (Match match in matchs3) { Console.WriteLine(match.Value);//output
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注