求两个正则表达式!
//[{ "Name ": "dd ", "Number ":123},{ "Name ": "d}\ "d ", "Number ":123}]
第一个正则:
这样一段json数据,希望第一次返回:
{ "Name ": "dd ", "Number ":123}和{ "Name ": "d}\ "d ", "Number ":123}
第二个正则:
对于{ "Name ": "d}\ "d ", "Number ":123}
返回 Name 和 d}\ "d 和 Number 和 123
多谢!!
------解决方案--------------------那就比较困难了
帮你Up~
下班了明天有空过来细看
------解决方案--------------------测试字符串是这样的,包括你上面列举的各种情况
[{ "Name ": "dd ", "Number ":123, "Id ": "{} ", "Name ": "dd ", "Number ":123},{ "Name ": "d}\ "d ", "Number ":123},{ "Name ": "ddd ", "Number ":222}]
我的理解是,{}中全都是这样的结构
"... ": "... "或者当:后为数字时, "... ":...,可能为一个或多个,多个时中间用,隔开,不知道是不是这样
上面的第一次取出结果两侧少了{和},加上
string test = "[{\ "Name\ ":\ "dd\ ",\ "Number\ ":123,\ "Id\ ":\ "{}\ ",\ "Name\ ":\ "dd\ ",\ "Number\ ":123},{\ "Name\ ":\ "d}\\\ "d\ ",\ "Number\ ":123},{\ "Name\ ":\ "ddd\ ",\ "Number\ ":222}] ";
MatchCollection mc1 = Regex.Matches(test, @ "\{( " ".*?(? <!\\) " ":( " ".*?(? <!\\) " "|\d+))(,( " ".*?(? <!\\) " ":( " ".*?(? <!\\) " "|\d+)))*\} ");
foreach (Match m1 in mc1)
{
richTextBox2.Text += m1.Value + "\n\n "; //第一次取出结果
MatchCollection mc2 = Regex.Matches(m1.Value, @ " " "(? <key> .*?(? <!\\)) " ":( " "(? <value> .*?(? <!\\)) " "|(? <value> \d+)) ");
foreach (Match m2 in mc2)
{
richTextBox2.Text += m2.Groups[ "key "].Value + "\n "; //这两项为第二次取出结果
richTextBox2.Text += m2.Groups[ "value "].Value + "\n ";
}
richTextBox2.Text += "\n ";
}
我在winform下写的代码^o^,webform下测试自己改下吧