【发布时间】: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