Mark
Mark

Reputation: 7818

LINQ or REGEX to extract certain text from a string

I have a string in my C# model populated with this string:

"[{\"ta_id\":97497,\"partner_id\":\"229547\",\"partner_url\":\"http://partner.com/deeplink/to/229547\"},{\"ta_id\":97832,\"partner_id\":\"id34234\",\"partner_url\":\"http://partner.com/deeplink/to/id34234\"}]"

Is there a way, using LINQ or RegEx, that I could parse out the partner_id's - so I ended up with a list object with:

229547
id34234

Thanks for your help, Mark

Upvotes: 0

Views: 720

Answers (4)

Kendall Frey
Kendall Frey

Reputation: 44326

Since this is JSON, you probably shouldn't bother trying to get a regex working. Instead, you can parse the JSON and then use LINQ.

using System.Web.Script.Serialization; // (in System.Web.Extensions.dll)

...

string s = "[{\"ta_id\":97497,\"partner_id\":\"229547\",\"partner_url\":\"http://partner.com/deeplink/to/229547\"},{\"ta_id\":97832,\"partner_id\":\"id34234\",\"partner_url\":\"http://partner.com/deeplink/to/id34234\"}]";
JavaScriptSerializer j = new JavaScriptSerializer();
object[] objects = (object[])j.DeserializeObject(s);
string[] ids = objects.Cast<Dictionary<string, object>>()
                      .Select(dict => (string)dict["partner_id"])
                      .ToArray();

It's a little messy to deserialize it to an object, because you don't have any type information. If you're not afraid of making a small class to deserialize into, you can do something like this:

class Foo
{
    public string partner_id
    {
        get;
        set;
    }
}

...

JavaScriptSerializer j = new JavaScriptSerializer();
string[] ids = j.Deserialize<Foo[]>(s).Select(x => x.partner_id).ToArray();

Note that there are other options for deserializing JSON. I simply chose the most general-purpose one that's built in.

Upvotes: 0

Srb1313711
Srb1313711

Reputation: 2047

If that is the general format of the string then this regex should work

(?i)(?<=(partner_id).{5})\w+

Test here

This from your string will get

229547 and id34234

(?i) = Case insesitivity

(?<=(partner_id).{5}) = postive lookbehind for parter_id then any 5 characters which in this case will be \":\"

\w+ = Any alphanumeric characters one or more times

Hope this helped

Upvotes: 2

Tafari
Tafari

Reputation: 3069

I have never used any JSON parser but if it comes to Regex you could try something like this:

private static void regexString()
{
    string myString = "[{\"ta_id\":97497,\"partner_id\":\"229547\",\"partner_url\":\"http://partner.com/deeplink/to/229547\"},{\"ta_id\":97832,\"partner_id\":\"id34234\",\"partner_url\":\"http://partner.com/deeplink/to/id34234\"}]";

    string[] stringList = Regex.Split(myString, "},{");

    for (int i=0; i<stringList.Length ;i++)
    {
        stringList[i] = Regex.Split(Regex.Split(stringList[i], "partner_id\\\":\\\"")[1], "\\\",\\\"partner_url\\\"")[0];
    }
}

Also there is a nice website to help you with creating your own regex patterns in the future, check it out:

gskinner.com

And a nice and short tutorial:

www.codeproject.com

Upvotes: 2

monu
monu

Reputation: 370

Assuming your link having partner id always-

   string Name = "[{\"ta_id\":97497,\"partner_id\":\"229547\",\"partner_url\":\"http://partner.com/deeplink/to/229547\"},{\"ta_id\":97832,\"partner_id\":\"id34234\",\"partner_url\":\"http://partner.com/deeplink/to/id34234\"}]";

        string[] splittedString = Regex.Split(Name, "}");
        List<string> allIds = new List<string>();
        foreach (var i in splittedString)
        { 
            var ids =Regex.Split(i, "/");
            string id = ids[ids.Length - 1];
            allIds.Add(id);
        }

Upvotes: 2

Related Questions