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

Removing a string using regular expression with stringr

我正在尝试使用 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"},

有什么建议吗?

如果我猜对了,您打算从 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)

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

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

DEMO