0

我有一个大字符串。这是其中的一部分:

{"status":"ok","items":[{"image_versions":[{"url":"http:\/\/distilleryimage8.instagram.com\/11a67042c62311e1bf341231380f8a12_7.jpg","width":612,"type":7,"height":612},{"url":"http:\/\/distilleryimage8.instagram.com\/11a67042c62311e1bf341231380f8a12_6.jpg","width":306,"type":6,"height":306},{"url":"http:\/\/distilleryimage8.instagram.com\/11a67042c62311e1bf341231380f8a12_5.jpg","width":150,"type":5,"height":150}],"code":"MrMBxJo-O8","has_more_comments":true,"taken_at":1341438972.0,"comments":[{"media_id":228329104165036988,"_spam":false,"text":"I live in Oklahoma! :D Shoot them off with me! :D","created_at":1341441914.0,"user":{"username":"heather_all_over","pk":13296276,"profile_pic_url":"http:\/\/images.instagram.com\/profiles\/profile_13296276_75sq_1339538236.jpg","full_name":"Heather\ud83c\udf80","is_private":false},"content_type":"comment","pk":228353791620276525,"type":0},{"media_id":228329104165036988,"_spam":false,"text":"Wish I had that much money to spend.......","created_at":1341441916.0,"user":{"username":"l_mcnair","pk":23775741,"profile_pic_url":"http:\/\/images.instagram.com\/profiles\/profile_23775741_75sq_1339894045.jpg","full_name":"Lauryn","is_private":true},"content_type":"comment","pk":228353803204944174,"type":0},{"media_id":228329104165036988,"_spam":false,"text":"You should video tape you setting them all off","created_at":1341441939.0,"user":{"username":"ahrii_","pk":37732021,"profile_pic_url":"http:\/\/images.instagram.com\/profiles\/profile_37732021_75sq_1340907381.jpg","full_name":"Ahriana;-*","is_private":false},"content_type":"comment","pk":228353997065675057,"type":0},{"media_id":228329104165036988,"_spam":false,"text":"When did skrillex start selling 

我正在尝试匹配“pk”之后的每个数字:“。我一直在尝试向前看,但似乎不太正确。我对正则表达式了解不多,所以如果有人能指出我正确的方向会很好!

4

3 回答 3

4

这看起来像一个 JSON 响应。为什么不只解析 JSON 并提取所有“pk”键的值?

于 2012-07-05T03:11:29.777 回答
2

根据您使用的语言,正则表达式可能看起来不同,但这应该适用于大多数语言:

/"pk":(\d+)/g

这基本上是查找字符串“pk”:然后是之后的所有数字,将这些数字放在捕获组中。最后的 g 使它搜索所有出现的事件。但是,根据您使用的语言,您可能无法检索所有捕获。

于 2012-07-05T03:18:04.217 回答
1

如果你想在某事之后的部分,你应该使用后视:

(?<="pk":)\d+
于 2012-07-05T03:10:23.900 回答