Regex.Match简单数组

时间:2012-10-20 19:51:50

标签: c# regex

以下是我正在使用的内容。

{"allCharacters":[{"id":"410199","name":"D4rkness1939pwnz","gender":"1","skipLevel":"1","role":"alt","level":"30","generation":"1","xp":"4420578","gold":"2593","xpNextLevel":8735843},{"id":"250004","name":"Fallen_Sir_Illuminati","gender":"0","skipLevel":"0","role":"alt","level":"1","generation":"1","xp":"0","gold":"100","xpNextLevel":3000},{"id":"402615","name":"PRO_Illuminati","gender":"0","skipLevel":"0","role":"alt","level":"1","generation":"1","xp":"0","gold":"100","xpNextLevel":3000},{"id":"146314","name":"PRO_Illuminati_BRD","gender":"1","skipLevel":"0","role":"main","level":"25","generation":"17","xp":"1897767","gold":"375492","xpNextLevel":1929158},{"id":"342584","name":"PRO_Sir_Illuminati","gender":"0","skipLevel":"0","role":"alt","level":"1","generation":"1","xp":"2000","gold":"200","xpNextLevel":3000},{"id":"252818","name":"Sir_Ashton_of_Illuminati","gender":"0","skipLevel":"0","role":"alt","level":"1","generation":"1","xp":"2000","gold":"200","xpNextLevel":3000},{"id":"336515","name":"Sir_Illuminati","gender":"0","skipLevel":"0","role":"alt","level":"1","generation":"1","xp":"0","gold":"100","xpNextLevel":3000}],"currentCharacter":0}

我正在尝试获取PRO_Illuminati_BRD的一个,角色“:”主要部分是正则表达式匹配中最具体的部分。 所以基本上就是这条线。

{"id":"146314","name":"PRO_Illuminati_BRD","gender":"1","skipLevel":"0","role":"main","level":"25","generation":"17","xp":"1897767","gold":"375492","xpNextLevel":1929158},

以下是我使用过的所有Regex.Match,但它们都只适用于D4rkness1939pwnz。

Match RealMain = Regex.Match(RawRes, "\"id\":\"(.*?)\",\"name\":\"(.*?)\",\"gender\":\"[0-9]\",\"skipLevel\":\"0\",\"role\":\"main\"");

Match RealMain = Regex.Match(RawRes, "\"id\":\"(.*?)\",\"name\":\"(.*?)\",\"gender\":\"(.*?)\",\"skipLevel\":\"0\",\"role\":\"main\"");

Match RealMain = Regex.Match(RawRes, "\"id\":\"(.*?)\",\"name\":\"(.*?)\",\"gender\":\"[0-9]\",\"skipLevel\":\"0\",\"role\":\"main\",\"level\":\"(.*?)\",\"generation\":\"(.*?)\",\"xp\":\"(.*?)\",\"gold\":\"(.*?)\",\"xpNextLevel\"");

Match RealMain = Regex.Match(RawRes, "\"id\":\"(.*?)\",\"name\":\"(.*?)\",\"gender\":\"(.*?)\",\"skipLevel\":\"0\",\"role\":\"main\",\"level\":\"(.*?)\",\"generation\":\"(.*?)\",\"xp\":\"(.*?)\",\"gold\":\"(.*?)\",\"xpNextLevel\"");

但他们总是返回D4rkness1939pwnz。 :S

我哪里错了?

2 个答案:

答案 0 :(得分:1)

如何使用json parser代替正则表达式

string json = @"{""allCharacters"":[{""id"":""410199"",""name"":""D4rkness1939pwnz"",""gender"":""1"",""skipLevel"":""1"",""role"":""alt"",""level"":""30"",""generation"":""1"",""xp"":""4420578"",""gold"":""2593"",""xpNextLevel"":8735843},{""id"":""250004"",""name"":""Fallen_Sir_Illuminati"",""gender"":""0"",""skipLevel"":""0"",""role"":""alt"",""level"":""1"",""generation"":""1"",""xp"":""0"",""gold"":""100"",""xpNextLevel"":3000},{""id"":""402615"",""name"":""PRO_Illuminati"",""gender"":""0"",""skipLevel"":""0"",""role"":""alt"",""level"":""1"",""generation"":""1"",""xp"":""0"",""gold"":""100"",""xpNextLevel"":3000},{""id"":""146314"",""name"":""PRO_Illuminati_BRD"",""gender"":""1"",""skipLevel"":""0"",""role"":""main"",""level"":""25"",""generation"":""17"",""xp"":""1897767"",""gold"":""375492"",""xpNextLevel"":1929158},{""id"":""342584"",""name"":""PRO_Sir_Illuminati"",""gender"":""0"",""skipLevel"":""0"",""role"":""alt"",""level"":""1"",""generation"":""1"",""xp"":""2000"",""gold"":""200"",""xpNextLevel"":3000},{""id"":""252818"",""name"":""Sir_Ashton_of_Illuminati"",""gender"":""0"",""skipLevel"":""0"",""role"":""alt"",""level"":""1"",""generation"":""1"",""xp"":""2000"",""gold"":""200"",""xpNextLevel"":3000},{""id"":""336515"",""name"":""Sir_Illuminati"",""gender"":""0"",""skipLevel"":""0"",""role"":""alt"",""level"":""1"",""generation"":""1"",""xp"":""0"",""gold"":""100"",""xpNextLevel"":3000}],""currentCharacter"":0}";
var obj = new JavaScriptSerializer().Deserialize<Root>(json);

//var character = obj.allCharacters.First(i => i.name == "PRO_Illuminati_BRD");
var character = obj.allCharacters.First(i => i.role == "main");

-

public class Root
{
    public List<AnItem> allCharacters;
}

public class AnItem
{
    public string id;
    public string name;
    public string gender;
    public string skipLevel;
    public string role;
    public string generation;
    public string xp;
    public string gold;
    public int xpNextLevel;
}

答案 1 :(得分:0)

您遇到的问题是您只阅读第一场比赛。

要遍历所有匹配项,您应该使用匹配项:

MatchCollection matches = Regex.Matches(RawRes, "\"id\":\"(.*?)\",\"name\":\"(.*?)\",\"gender\":\"[0-9]\",\"skipLevel\":\"0\",\"role\":\"main\"");
foreach(Match item in matches)
{
    if (item.Groups[2].Value == "PRO_Illuminati_BRD")
        return item;
}

或者你应该将你的正则表达式改为:

Regex.Matches(RawRes, "^.*\"id\":\"(.*?)\",\"name\":\"PRO_Illuminati_BRD\",\"gender\":\"[0-9]\",\"skipLevel\":\"0\",\"role\":\"main\"");

这回答了您的问题,但我建议您使用其他人已经描述的JSON解析器,如果您需要的项目多于单个项目。