日期:2014-05-18  浏览次数:20910 次

求一正则 提取字符串
字符串:
C# code
[[-1,"bjs","北京",123432,"AAEE66",[[11561,"bj","aacc55","bjs","tt"],

[11561,"Q","aacc55","bjs","QUINELLA"]],"中国"],
[-1,"AG","梅特兰s",123436,"FF88CC",[[11464,"AU","dd88cc","AUs","dd"]],"澳洲"]]


提取出:
  北京 11561 bj 中国
  北京 11561 Q 中国
  梅特兰s 11464 AU 澳洲

------解决方案--------------------
只有一个 "中国" ,怎么会提取出两个出来...
------解决方案--------------------
解析JSON会比较容易
------解决方案--------------------
数据样本较少,不一定符合所有情况

C# code
string test = @"[[-1,""bjs"",""北京"",123432,""AAEE66"",[[11561,""bj"",""aacc55"",""bjs"",""tt""],

[11561,""Q"",""aacc55"",""bjs"",""QUINELLA""]],""中国""],
[-1,""AG"",""梅特兰s"",123436,""FF88CC"",[[11464,""AU"",""dd88cc"",""AUs"",""dd""]],""澳洲""]]";
Regex reg = new Regex(@"\[-1,[^,]+,""([^""]*)"",[^\[]+\[(?:\[(\d+),""([^""]*)""[^\]]*\],?\s*)*\],""([^""]*)""\]");
MatchCollection mc = reg.Matches(test);
foreach (Match m in mc)
{
    foreach (Capture c in m.Groups[3].Captures)
    {
        richTextBox2.Text += m.Groups[1].Value + " " + m.Groups[2].Value + " " + c + " " + m.Groups[4].Value + "\n";
    }
}
/*-----输出-----
北京 11561 bj 中国
北京 11561 Q 中国
梅特兰s 11464 AU 澳洲
*/