2

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?

4
  • 3
    this looks like JSON to me. Why not use a JSON parser such as JSON.NET? Commented Oct 20, 2012 at 19:54
  • I just want to grab that line with regex, can you help? Commented Oct 20, 2012 at 19:56
  • I'm not good with regular expressions. I'd strongly suggest using a tool such as Regex Tester to help find the right expression. Commented Oct 20, 2012 at 19:59
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Oct 20, 2012 at 20:43

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

10 Comments

What reference would I use for C# .net 4.0 windows form? Looks awesome.
I found it :D Namespace: System.Web.Script.Serialization Assembly: System.Web.Extensions (in System.Web.Extensions.dll)
I am using string json = @RawRes; var obj = new JavaScriptSerializer().Deserialize<Root>(json); var character = obj.allCharacters.First(i => i.role == "main"); MessageBox.Show(character.ToString()); since the RawRes changes for different logged in users, I want to grab the name based on the role being main
I did use var character = obj.allCharacters.First(i => i.role == "main"); MessageBox.Show(character.ToString()); and it doesnt work
|
0

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.

3 Comments

When I use, MatchCollection matches = Regex.Matches(RawRes, "\"id\":\"[0-9]{1,20}\",\"name\":\"(.*?)\",\"gender\":\"[0-9]{1}\",\"skipLevel\":\"0\",\"role\":\"main"); the match gets all of this crap in matches[0].ToString();
"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
Instead of .*? you should use [^\"]* E.g. "\"id\":\"([^\"]*)\",\"name\":\"([^\"]*)\",\"gender\":\"[0-9]\",\"skipLevel\":\"0\",\"role\":\"main\""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.