0

I have trouble doing a regexp, and I don't know how to formulate the questions, so better than words, here is an example

String

"medias": [
    {
      "height": 800,
      "id": "",
      "origin": "google",
      "thumbnail": {
        "height": 94,
        "url": "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTF_KeDfHAxLuvCoYkuKUcwPKpxHfjt19g-E0uhsV20rLGf6VbJ--NFKCuO",
        "width": 150
      },
      "title": "144779-cat-cats.jpg",
      "type": "image",
      "url": "https://lh4.googleusercontent.com/-bemqYIv9dPw/UTsFD0yJsBI/AAAAAAAAAZI/1zrKmdolPLY/s0-d/144779-cat-cats.jpg",
      "width": 1280
    },
    {
      "height": 300,
      "id": "",
      "origin": "google",
      "thumbnail": {
        "height": 94,
        "url": "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcR60TwME2D02rjeROsOlHSMdk5AkrtwPwJIhsXBqoOoISUA95rXpKXihL4",
        "width": 124
      },
      "title": "cats-in-sink.jpeg",
      "type": "image",
      "url": "https://lh4.googleusercontent.com/-zrZJg2qJQlI/Tg9kwdqbsrI/AAAAAAAAAEQ/Oal8KLLfItk/cats-in-sink.jpeg",
      "width": 397
    }
]

Regexp

"medias": \[
   ([{
      "height": \d+[,|]
      "id": ".*"[,|]
      "origin": ".*"[,|]
      "thumbnail": {
        "height": \d+[,|]
        "url": ".*"[,|]
        "width": \d+[,|]
      }[,|]
      "title": ".*"[,|]
      "type": ".*"[,|]
      "url": ".*"[,|]
      "width": \d+[,|]
    }[,|]]*)[,|]
\][,|]

Just the middle part of the regexp, between (), works fine. but when I add the media wrapper around it gets broken.

"medias": \[
 ....................
 ....................
 ...........[,|]
\][,|]

Here you'll find the middle part matching

And here you'll find the complete regexp that is broken

Any Idea ?

1
  • 2
    Using a huge regex to match something like that is asking for trouble. JSON has a well-defined format; you'd probably find it a lot easier to use a JSON parser instead of a regex. Commented Jun 18, 2013 at 16:08

1 Answer 1

1

Use the function json_decode() in php instead

$array = json_decode($json_object);
Sign up to request clarification or add additional context in comments.

2 Comments

You can't know because i didn't mention it in the question but i'm actually using ruby. So I should do: hash = JSON.parse(myjsonstring) A good post here
Thanks. It seems everything is under control :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.