C#正则表达式整理备忘
C#正则表达式整理备忘
(1)“@”符号 (2)基本的语法字符。
C# Code:
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
C# 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通常用于约束一个完整的单词
Code C# Code:
string x = "1024"; string y = " 1024"; string z = "1,024"; 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的整数。
Code C# 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 r = new Regex(@"^\ ?((100(.0 )*)|([1-9]?[0-9])(\.\d )*)$"); 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);//1 Console.WriteLine("c match count:" r.Matches(c).Count);//1 Console.WriteLine("d match count:" r.Matches(d).Count);//0 Console.WriteLine("e match count:" r.Matches(e).Count);//0
Code C# Code:
string x = "\\"; Regex r1 = new Regex("^\\\\$"); Console.WriteLine("r1 match count:" r1.Matches(x).Count);//1 Regex r2 = new Regex(@"^\\$"); Console.WriteLine("r2 match count:" r2.Matches(x).Count);//1 Regex r3 = new Regex("^\\$"); Console.WriteLine("r3 match count:" r3.Matches(x).Count);//0 //匹配“\” string x = "\""; Regex r1 = new Regex("^\"$"); Console.WriteLine("r1 match count:" r1.Matches(x).Count);//1 Regex r2 = new Regex(@"^""$"); Console.WriteLine("r2 match count:" r2.Matches(x).Count);//1 //匹配双引号
(7)组与非捕获组 Code C# Code:
string x = "Live for nothing,die for something"; string y = "Live for nothing,die for somebody"; Regex r = new Regex(@"^Live ([a-z]{3}) no([a-z]{5}),die \1 some\2$"); Console.WriteLine("x match count:" r.Matches(x).Count);//1 Console.WriteLine("y match count:" r.Matches(y).Count);//0 //正则表达式引擎会记忆“()”中匹配到的内容,作为一个“组”,并且可以通过索引的方式进行引用。表达式中的“\1”,用于反向引用表达式中出现的第一个组,即粗体标识的第一个括号内容,“\2”则依此类推。 string x = "Live for nothing,die for something"; Regex r = new Regex(@"^Live for no([a-z]{5}),die for some\1$"); if (r.IsMatch(x)) { Console.WriteLine("group1 value:" r.Match(x).Groups[1].Value);//输出:thing } //获取组中的内容。注意,此处是Groups[1],因为Groups[0]是整个匹配的字符串,即整个变量x的内容。 string x = "Live for nothing,die for something"; Regex r = new Regex(@"^Live for no(?<g1>[a-z]{5}),die for some\1$"); if (r.IsMatch(x)) { Console.WriteLine("group1 value:" r.Match(x).Groups["g1"].Value);//输出:thing } //可根据组名进行索引。使用以下格式为标识一个组的名称(?<groupname>…)。 string x = "Live for nothing nothing"; Regex r = new Regex(@"([a-z] ) \1"); if (r.IsMatch(x)) { x = r.Replace(x, "$1"); Console.WriteLine("var x:" x);//输出:Live for nothing } //删除原字符串中重复出现的“nothing”。在表达式之外,使用“$1”来引用第一个组,下面则是通过组名来引用: string x = "Live for nothing nothing"; Regex r = new Regex(@"(?<g1>[a-z] ) \1"); if (r.IsMatch(x)) { x = r.Replace(x, "${g1}"); Console.WriteLine("var x:" x);//输出:Live for nothing } string x = "Live for nothing"; Regex r = new Regex(@"^Live for no(?:[a-z]{5})$"); if (r.IsMatch(x)) { Console.WriteLine("group1 value:" r.Match(x).Groups[1].Value);//输出:(空) } //在组前加上“?:”表示这是个“非捕获组”,即引擎将不保存该组的内容。
Code C# Code:
string x = "Live for nothing,die for something"; Regex r1 = new Regex(@".*thing"); if (r1.IsMatch(x)) { Console.WriteLine("match:" r1.Match(x).Value);//输出:Live for nothing,die for something } Regex r2 = new Regex(@".*?thing"); if (r2.IsMatch(x)) { Console.WriteLine("match:" r2.Match(x).Value);//输出:Live for nothing }
Code C# Code:
string x = "Live for nothing,die for something"; Regex r1 = new Regex(@".*thing,"); if (r1.IsMatch(x)) { Console.WriteLine("match:" r1.Match(x).Value);//输出:Live for nothing, } Regex r2 = new Regex(@"(?>.*)thing,"); if (r2.IsMatch(x))//不匹配 { Console.WriteLine("match:" r2.Match(x).Value); }
(10)正向预搜索、反向预搜索 Code Code C# Code:
string x = "used:1024 free:2048"; Regex r1 = new Regex(@"(?<=used:)\d{4}"); if (r1.Matches(x).Count==1) { Console.WriteLine("r1 match:" r1.Match(x).Value);//输出:1024 } Regex r2 = new Regex(@"(?<!used:)\d{4}"); if (r2.Matches(x).Count==1) { Console.WriteLine("r2 match:" r2.Match(x).Value);//输出:2048 }
(11)十六进制字符范围
Code C# Code:
Regex r = new Regex(@"^\ ?0*(?:100(\.0*)?|(\d{0,2}(?=\.\d)|\d{1,2}(?=($|\.$)))(\.\d*)?)$"); string x = ""; while (true) { x = Console.ReadLine(); if (x != "exit") { if (r.IsMatch(x)) { Console.WriteLine(x " succeed!"); } else { Console.WriteLine(x " failed!"); } } else { break; } }
参考文档:
C#使用正则表达式判断手机号码 C#.NET 身份证号码格式正则表达式验证 C#.NET IP地址正则表达式验证 C#.NET 手机号码正则表达式验证 VS表达式包含未定义的函数调用IsNullOrEmpty|C/S框架网 C#.NET通过CodeDom.Compiler动态编译计算表达式的值 C#正则表达式查找或替换指定字符串(标识)范围的内容 C#使用正则表达式移除所有的Html标记,返回纯文本 C#使用正则表达式替换或去掉半角标点符号 C# 从html中通过正则找到IP地址信息(只支持ipv4地址) C#类扩展方法,字符串对象转换,常用扩展方法StringExtensions C#构造函数(构造方法) C#方法重载(函数重载) C#正则表达式判断url是否网络路径(http/ftp/https三种协议) C#正则表达式替换SQL单引号中间的空格
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |