【发布时间】:2017-09-28 15:26:46
【问题描述】:
我正在尝试提取 JSON 对象的正文字符串中的内容。问题是我无法将双引号传递给并提取所有文本信息。在这种情况下,我需要文本为“文档中存在关于库函数链接的拼写错误,这在浏览文档时非常烦人!”。我已经把我的代码放在这里,如果有人可以建议如何传递双引号而不是 (",) 并且可以达到这个贪婪的量词,(这是我一直在使用的表达式),
body<- str_extract(json_file , 'body[^,]*\\s*').
谢谢
library(stringr)
json_file<- '{ "_id" : ObjectId( "539163d7bd350003" ), "login" : "vui", "id" : 369607, "avatar_url" : "https://avatars.mashupsusercontent.com/u/369607?", "gravatar_id" : "df8897ffebe16c5b0cd690925c63e190", "body":"There is a typo error in documentation regarding a link to a librarys function, which is quite irritating while browsing the documentation!","url" : "https://api.mashups.com/users/vui", "html_url" : "https://mashups.com/vui", "followers_url" : "https://api.mashups.com/users/vui/followers", "following_url" : "https://api.mashups.com/users/vui/following{/other_user}", "gists_url" : "https://api.mashups.com/users/vui/gists{/gist_id}", "starred_url" : "https://api.mashups.com/users/vui/starred{/owner}{/repo}", "subscriptions_url" : "https://api.mashups.com/users/vui/subscriptions", "organizations_url" : "https://api.mashups.com/users/vui/orgs", "repos_url" : "https://api.mashups.com/users/vui/repos", "events_url" : "https://api.mashups.com/users/vui/events{/privacy}", "received_events_url" : "https://api.mashups.com/users/vui/received_events", "type" : "User", "site_admin" : false, "org" : "amurath"}'
body<- str_extract(json_file , 'body[^,]*\\s*')
body
【问题讨论】:
-
给定的字符串不是有效的 JSON 对象。所以你有一个仍然是 JSONish 的字符串。除非您无法定义 JSONish 的含义,否则任何引用模式在您眼中都可能是有效的。
-
我放了一个经过验证的 JSON 对象,如果您有任何想法,请告诉我!谢谢,问候!
标签: r regex string-matching stringr