使用带有stringr的正则表达式删除字符串

时间:2015-06-22 18:44:21

标签: regex r stringr jsonlite

我试图使用str_replace_all函数在以下json中删除术语union。我尝试了几个正则表达式,但它们没有用。关于如何摆脱完整的“联合”字符串的任何建议。这是代码。

 library(jsonlite)

 json<- '{"open_issues":0,"union":{"avatar_url":"https://secure.gravatar.com/avatar/ed73cf7f24c091318e507d167c7b3456?d=https://a248.e.akamai.net/assets.mashups.com%2Fimages%2Fgravatars%2Fgravatar-orgs.png","gravatar_id":"ed73cf7f24c091318e507d167c7b3456","id":1556245,"login":"Toolpark","url":"https://api.mashups.com/users/Toolpark"},"owner":{"avatar_url":"https://secure.gravatar.com/avatar/ed73cf7f24c091318e507d167c7b3456?d=https://a248.e.akamai.net/assets.mashups.com%2Fimages%2Fgravatars%2Fgravatar-orgs.png","gravatar_id":"ed73cf7f24c091318e507d167c7b3456","id":1556245,"login":"Toolpark","url":"https://api.mashups.com/users/Toolpark"},"private":false,"pushed_at":"2012-07-12T11:43:31Z","size":964,"ssh_url":"vit@mashups.com:Toolpark/irma.vit","svn_url":"https://mashups.com/Toolpark/irma","updated_at":"2012-07-12T11:43:31Z","url":"https://api.mashups.com/repos/Toolpark/irma","watchers":2}'

str_replace_all(json,"union[^}]*}\s*, "")
json <- fromJSON(json)
Error: '\s' is an unrecognized escape in character string starting ""union[^}]*}\s"

我正试图摆脱这个字符串,最后包括昏迷:

"union":{"avatar_url":"https://secure.gravatar.com/avatar/ed73cf7f24c091318e507d167c7b3456?d=https://a248.e.akamai.net/assets.mashups.com%2Fimages%2Fgravatars%2Fgravatar-orgs.png","gravatar_id":"ed73cf7f24c091318e507d167c7b3456","id":1556245,"login":"Toolpark","url":"https://api.mashups.com/users/Toolpark"},

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

如果我猜测正确,你打算从json行中删除“union”元素。那么,为什么不使用 fromJSON 函数将行更改为json,然后尝试删除 union 元素。以下代码为您生成一个没有 union

的列表
json1 <- ("your json line") 
my_list<- fromJSON(json1)   #convert into json format which gives you a list of elements
my_list["union"]<- NULL   #delete the union element
print(my_list)

答案 1 :(得分:1)

您一直在使用语法错误的正则表达式。请使用以下命令:

str_replace_all(json, "\"union[^}]*}\\s*,", "")

请参阅DEMO