Ashton Schultz
Ashton Schultz

Reputation: 23

Regex.Match Simple array

Here is what I am using.

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

I'm trying to get the one for PRO_Illuminati_BRD, with the role":"main part being the most specific part of the regex match. So basically this line specifically.

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

Below are all the Regex.Match I have used, but they all only match for D4rkness1939pwnz.

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

and

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

and

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

and

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

but they always return D4rkness1939pwnz one. :S

Where am I going wrong?

Upvotes: 2

Views: 175

Answers (2)

Casperah
Casperah

Reputation: 4554

The problem you are facing is that you only read the first Match.

To loop through all the matches you should use 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;
}

Alternatively you should change your regex to something like this:

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

This answers your question, but I recommend that you use the JSON parser that others already have described if you need more items than just the single item.

Upvotes: 0

L.B
L.B

Reputation: 116118

How about using a json parser instead of regex

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

Upvotes: 1

Related Questions