gpt4 book ai didi

c# - Regex.Match 简单数组

转载 作者:太空宇宙 更新时间:2023-11-03 13:53:44 25 4
gpt4 key购买 nike

这是我正在使用的。

{"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

我哪里错了?

最佳答案

使用 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;
}

关于c# - Regex.Match 简单数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12992094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com