Florian
Florian

Reputation: 5994

Regex between quotes

I have to parse as part of a project a playlist file:

The layout looks like this:

{
         "info" : "",
         "time" : "05:00",
         "url_stream" : "http://loopstream01.apa.at/?channel=oe1&id=20120726_0500_1_2_nachrichten_XXX_w_",
         "day_label" : "26.07.2012",
         "short_title" : "Nachrichten",
         "url_detail" : "",
         "url_json" : "/programm/308178/konsole",
         "parts" : [],
         "tag" : "",
         "id" : "308178",
         "title" : "Nachrichten",
         "url_playlist" : "/programm/308178/playlist"
      },........... and so on

now i want to get the values of the "properties". I tried this one

"info" : "(?<info>(([^"]*)))", ....

but it is buggy because the it is possible that there is something like this:

"info" : "Hello "World" this was a test",

you see that "World" is also in "" and so it gets buggy. Does anyone has a good and clean solution for me?

Upvotes: 0

Views: 242

Answers (3)

Ωmega
Ωmega

Reputation: 43703

Try this one:

(?<=[\n\r])[^\S\n\r]*"info"[^\S\n\r]*:[^\S\n\r]*"(?<info>.*?)",?[^\S\n\r]*(?=[\n\r])

Upvotes: 0

L.B
L.B

Reputation: 116188

I found two similar pages on internet. They can be parsed using Json.Net as below:

using (var wc = new WebClient())
{
    string url = "http://derruki.dyndns.org/oe1rip/json-list-source.php";
    string json = wc.DownloadString(url);

    dynamic dynObj = JsonConvert.DeserializeObject(json);
    foreach(var item in dynObj)
    {
        Console.WriteLine("INFO:{0}\nTITLE:{1}\nURL:{2}\n\n",
            item.info, item.short_title, item.url_stream);
    }
}

For http://oe1.orf.at/programm/konsole/tag/20120726 you should change for loop as

foreach(var item in dynObj.list)

Upvotes: 0

woz
woz

Reputation: 11004

Use the JavaScriptSerializer class, documented by Microsoft here, to deserialize the JSON. That will be much easier than RegEx.

Upvotes: 2

Related Questions