Regexp,无法匹配数组中包含的许多元素(json)

时间:2013-06-18 16:07:02

标签: ruby regex json pattern-matching

我在制作正则表达式时遇到了麻烦,我不知道如何制定问题,所以比言语更好,这里有一个例子

字符串

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

的正则表达式

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

只是正则表达式的中间部分,在()之间,工作正常。但当我添加media wrapper时,它会被破坏。

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

Here you'll find the middle part matching

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

任何想法?

1 个答案:

答案 0 :(得分:1)

使用php中的函数json_decode()

$array = json_decode($json_object);