Sql中使用正则
下面我们来举例说明一下:
例1,查询name字段中包含有“明”字的。
select * from table1 where name like '%明%'
例3,查询name字段中含有数字的。
select * from table1 where name like '%[0-9]%'
例4,查询name字段中含有小写字母的。
select * from table1 where name like '%[a-z]%'
例5,查询name字段中不含有数字的。
select * from table1 where name like '%[!0-9]%'
去空格
string trim = Regex.Replace( text, @"\s", "" );
找出sql中的参数
Regex paramReg = new Regex(@"[^@@](?<p>@\w+)");
Regex paramReg = new Regex(@"@\w*");
MatchCollection mathches= paramReg.Matches(strSql);
1-8位数字截取匹配和整体匹配
Regex re = new Regex(@"^[0-9]{1,8}"); //截取匹配
Regex regex = new Regex(@"^[0-9]{1,8}$"); //整体匹配
//匹配字符串:m1.large(vcpu 2,ram 4G)|c95f5529-47e8-46d4-85da-319eb9905a9b.
//目标字符串:vcpu: “2”, ram: “4”, id:“c95f5529 - 47e8 - 46d4 - 85da - 319eb9905a9b”.
var flavorRef = "m1.large(vcpu 2,ram 4G)|c95f5529-47e8-46d4-85da-319eb9905a9b";
Regex regex = new Regex(@"(?<= )[^ \,G]+(?=\,|G)|(?<=\|)[^\|]+");
MatchCollection mc = regex.Matches(flavorRef);
var cpu = mc[0].Value; //cpu:2
var ram = mc[1].Value; //ram:4
var id = mc[2].Value; //flavorId:c95f5529-47e8-46d4-85da-319eb9905a9b