新聞中心
C#正則表達式之定位字符都有哪些呢?讓我們來看看:

公司主營業(yè)務:成都做網(wǎng)站、成都網(wǎng)站建設、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出青縣免費做網(wǎng)站回饋大家。
“定位字符”所代表的是一個虛的字符,它代表一個位置,你也可以直觀地認為“定位字符”所代表的是某個字符與字符間的那個微小間隙。
^ 表示其后的字符必須位于字符串的開始處
$ 表示其前面的字符必須位于字符串的結束處
\b 匹配一個單詞的邊界
\B 匹配一個非單詞的邊界
另外,還包括:\A 前面的字符必須位于字符處的開始處,\z 前面的字符必須位于字符串的結束處,\Z 前面的字符必須位于字符串的結束處,或者位于換行符前
下面提供一些簡單的C#正則表達式之定位字符示例:
- 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
- //對于一個多行字符串,在設置了Multiline選項之后,^和$將出現(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通常用于約束一個完整的單詞
C#正則表達式之定位字符使用的基本內(nèi)容就向你介紹到這里,希望對你了解和學習C#正則表達式有所幫助。
分享題目:C#正則表達式之定位字符淺析
網(wǎng)頁URL:http://www.dlmjj.cn/article/dhscdpp.html


咨詢
建站咨詢
