程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#正則表達式整理備忘(2)

C#正則表達式整理備忘(2)

編輯:關於C語言

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

下面提供一些簡單的示例:

Code

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選項之後,^和$將出現多次匹配。
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通常用於約束一個完整的單詞

(4)重復描述字符

“重復描述字符”是體現C#正則表達式“很好很強大”的地方之一:

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

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

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

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

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

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved