当前位置:文档之家› C#中使用正则表达式详解

C#中使用正则表达式详解


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 次 以下提供一些简单的示例: Code string x = "1024"; string y = "+1024"; string z = "1,024";
下面提供一些简单的示例: 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).则表达式的“成员”,但是它经常与 C#正则表达式出 双入对。“@”表示,跟在它后面的字符串是个“逐字字符串”,不是很好理解, 举个例子,以下两个声明是等效的:
string x="D:\\My Huang\\My Doc";
string y = @"D:\My Huang\My Doc";
string a = "1"; string b="-1024"; string c = "10000"; Regex r = new Regex(@"^\+?[1-9],?\d{3}$"); Console.WriteLine("x match count:" + r.Matches(x).Count);//1 Console.WriteLine("y match count:" + r.Matches(y).Count);//1 Console.WriteLine("z match count:" + r.Matches(z).Count);//1 Console.WriteLine("a match count:" + r.Matches(a).Count);//0 Console.WriteLine("b match count:" + r.Matches(b).Count);//0 Console.WriteLine("c match count:" + r.Matches(c).Count);//0 //匹配 1000 到 9999 的整数。 (5)择一匹配 C#正则表达式中的 (|) 符号似乎没有一个专门的称谓,姑且称之为“择一 匹配”吧。事实上,像[a-z]也是一种择一匹配,只不过它只能匹配单个字符, 而(|)则提供了更大的范围,(ab|xy)表示匹配 ab 或匹配 xy。注意“|”与“()” 在此是一个整体。下面提供一些简单的示例: Code string x = "0"; string y = "0.23"; string z = "100"; string a = "100.01"; string b = "9.9"; string c = "99.9"; string d = "99."; string e = "00.1";
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 选项之后,^和$将出现多次匹 配。
相关主题