正则表达式学习

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值