2

これが私が使用しているものです。

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

どこが間違っていますか?

4

2 に答える 2

1

正規表現の代わりにjsonパーサーを使用するのはどうですか

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;
}
于 2012-10-20T20:00:33.333 に答える
0

あなたが直面している問題は、最初のマッチしか読んでいないことです。

すべての一致をループするには、Matches を使用する必要があります。

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 パーサーを使用することをお勧めします。

于 2012-10-20T20:19:49.680 に答える