日期:2014-05-17  浏览次数:20431 次

能不能用正则匹配多个值?
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);