溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#中怎么利用正則表達(dá)式定位字符

發(fā)布時(shí)間:2021-07-07 16:37:54 來(lái)源:億速云 閱讀:325 作者:Leah 欄目:編程語(yǔ)言

今天就跟大家聊聊有關(guān)C#中怎么利用正則表達(dá)式定位字符,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

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

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

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

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

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

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

下面提供一些簡(jiǎn)單的C#正則表達(dá)式之定位字符示例:

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  //對(duì)于一個(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è)完整的單詞

看完上述內(nèi)容,你們對(duì)C#中怎么利用正則表達(dá)式定位字符有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI