当前位置: 代码迷 >> ASP.NET >> 能不能用正则匹配多个值?解决办法
  详细解决方案

能不能用正则匹配多个值?解决办法

热度:9469   发布时间:2013-02-25 00:00:00.0
能不能用正则匹配多个值?
selfDiscountInfoId:'',discount:'40',urgency:'false',isApplyKSeat:'false'
比如这样,我想匹配discount后面的40,urgency后面的false,应该怎么处理?

------解决方案--------------------------------------------------------
用discount:\s*'(\d+)'\s*,\s*urgency:\s*'(\w+)'这个正则匹配,分组1是40,分组2的值是false
------解决方案--------------------------------------------------------
我来一个搓点的

C# code
string strInput = @"selfDiscountInfoId:'',discount:'40',urgency:'false',isApplyKSeat:'false'";        string strPattern = "discount:'([^']+)',urgency:'([^']+)'";        Regex reg = new Regex(strPattern);        Match m = reg.Match(strInput);        string strCount = m.Groups[1].Value;        string strGency = m.Groups[2].Value;
------解决方案--------------------------------------------------------
C# code
        string s = "selfDiscountInfoId:'',discount:'40',urgency:'false',isApplyKSeat:'false'";        Match match = Regex.Match(s, @"discount:'(?<discount>.*?)',urgency:'(?<urgency>.*?)'");        Response.Write(match.Groups["discount"].Value + "<br/>" + match.Groups["urgency"].Value);
  相关解决方案