能不能用正则匹配多个值?
selfDiscountInfoId:'',discount:'40',urgency:'false',isApplyKSeat:'false'
比如这样,我想匹配discount后面的40,urgency后面的false,应该怎么处理?
[解决办法]
用discount:\s*'(\d+)'\s*,\s*urgency:\s*'(\w+)'这个正则匹配,分组1是40,分组2的值是false
[解决办法]
我来一个搓点的
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;
[解决办法]
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);