【问题标题】:Regexp, can't match many elements contained in an array (json)正则表达式,无法匹配数组中包含的许多元素(json)
【发布时间】:2013-06-14 22:58:26
【问题描述】:

我在做正则表达式时遇到问题,而且我不知道如何制定问题,所以比文字更好,这里有一个例子

字符串

"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

有什么想法吗?

【问题讨论】:

  • 使用巨大的正则表达式来匹配这样的东西是自找麻烦。 JSON 具有明确定义的格式;您可能会发现使用 JSON 解析器而不是正则表达式要容易得多。

标签: ruby regex json pattern-matching


【解决方案1】:

改用php中的json_decode()函数

$array = json_decode($json_object);

【讨论】:

  • 你不知道,因为我没有在问题中提到它,但我实际上正在使用 ruby​​。所以我应该这样做:hash = JSON.parse(myjsonstring)一个好帖子here
  • 谢谢。似乎一切都在控制之中:)
猜你喜欢
  • 2016-12-08
  • 1970-01-01
  • 2017-10-16
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多