有一段时间,正则表达式学习很火热很潮流,当时在CSDN一天就能看到好几个正则表达式的帖子,那段时间借助论坛以及Wrox Press出版的《C#字符串和正则表达式参考手册》学习了一些基础的知识,同时也为我在CSDN大概赚了1000分,今天想起来,去找《C#字符串和正则表达式参考手册》时,已经不知所踪了。现在用到正则的时候也比较少,把以前的笔记等整理一下,以志不忘。 string i = "/n"; string m = "3"; Regex r = new Regex(@"/D"); //同Regex r = new Regex("//D"); //r.IsMatch(i)结果:true //r.IsMatch(m)结果:false string i = "%"; string m = "3"; Regex r = new Regex("[a-z0-9]"); //匹配小写字母或数字字符 //r.IsMatch(i)结果:false //r.IsMatch(m)结果:true
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通常用于约束一个完整的单词 |