用于替换 json 对象中的部分表达式的正则表达式
Regular expression to replace part of the expression in a json object
我正在尝试编写一个正则表达式来替换 json 文件对象中的一些有问题的字符。这是简短的 -DEMO
我能够编写 php 代码,但我无法在 R 中使用 str_replace 实现它,因为它以 ,{ 字符开头。关于如何改进代码有什么建议吗?
library(stringr)
json_file <- json_file<- '{"_id":{"$oid":"4f27779008d69a6dba0208f6"},"actor":{"gravatar_id":"92e5c51218f00220e0362c47b2a94b9a","id":NumberInt(228889),"login":"stefankendall","url":"https://api.github.com/users/stefankendall"},"created_at":"2012-01-31T05:09:37Z","id":"1515677813","org":{"url":"https://api.github.com/orgs/"},"payload":{"commits":[{"author":{"email":"skendall@skendalllaptop.(none)","name":"skendall"},"message":"Made test packages mimic app layout.","sha":"faf1b478f4d98202d4169b6d310812b14ad7f676","url":"https://api.github.com/repos/stefankendall/wendler531-webservices/commits/faf1b478f4d98202d4169b6d310812b14ad7f676"},{"author":{"email":"skendall@skendalllaptop.(none)","name":"skendall"},"message":"_id is now pulled out of \"get\" responses","sha":"d2087821e865ebebf9ff6e47cffb41dd16c6c871","url":"https://api.github.com/repos/stefankendall/wendler531-webservices/commits/d2087821e865ebebf9ff6e47cffb41dd16c6c871"}],"head":"d2087821e865ebebf9ff6e47cffb41dd16c6c871","push_id":NumberInt(59920001),"ref":"refs/heads/master","size":NumberInt(2)},"public":true,"repo":{"id":NumberInt(3186494),"name":"stefankendall/wendler531-webservices","url":"https://api.github.com/repos/stefankendall/wendler531-webservices"},"type":"PushEvent"}'
str_replace_all(json_file, "\,{"author[^*]*],\s*","")
- 您必须转义
"
或使用 '
作为包装器而不是 "
- 您必须在 R 中对控制字符进行两次转义,即
\{
、\^
,尤其是 \\
,因为 \
是 R 中的控制字符! (每个 \
都被 \
替换并传递给正则表达式解析器。
- 最终,
\s
必须替换为 [:space:]
参见 here。
你必须使用str_replace_all(json_file,',\{\"author[^\*]*\],\s*',"")
(即使对我来说没有多大意义......)
我正在尝试编写一个正则表达式来替换 json 文件对象中的一些有问题的字符。这是简短的 -DEMO
我能够编写 php 代码,但我无法在 R 中使用 str_replace 实现它,因为它以 ,{ 字符开头。关于如何改进代码有什么建议吗?
library(stringr)
json_file <- json_file<- '{"_id":{"$oid":"4f27779008d69a6dba0208f6"},"actor":{"gravatar_id":"92e5c51218f00220e0362c47b2a94b9a","id":NumberInt(228889),"login":"stefankendall","url":"https://api.github.com/users/stefankendall"},"created_at":"2012-01-31T05:09:37Z","id":"1515677813","org":{"url":"https://api.github.com/orgs/"},"payload":{"commits":[{"author":{"email":"skendall@skendalllaptop.(none)","name":"skendall"},"message":"Made test packages mimic app layout.","sha":"faf1b478f4d98202d4169b6d310812b14ad7f676","url":"https://api.github.com/repos/stefankendall/wendler531-webservices/commits/faf1b478f4d98202d4169b6d310812b14ad7f676"},{"author":{"email":"skendall@skendalllaptop.(none)","name":"skendall"},"message":"_id is now pulled out of \"get\" responses","sha":"d2087821e865ebebf9ff6e47cffb41dd16c6c871","url":"https://api.github.com/repos/stefankendall/wendler531-webservices/commits/d2087821e865ebebf9ff6e47cffb41dd16c6c871"}],"head":"d2087821e865ebebf9ff6e47cffb41dd16c6c871","push_id":NumberInt(59920001),"ref":"refs/heads/master","size":NumberInt(2)},"public":true,"repo":{"id":NumberInt(3186494),"name":"stefankendall/wendler531-webservices","url":"https://api.github.com/repos/stefankendall/wendler531-webservices"},"type":"PushEvent"}'
str_replace_all(json_file, "\,{"author[^*]*],\s*","")
- 您必须转义
"
或使用'
作为包装器而不是"
- 您必须在 R 中对控制字符进行两次转义,即
\{
、\^
,尤其是\\
,因为\
是 R 中的控制字符! (每个\
都被\
替换并传递给正则表达式解析器。 - 最终,
\s
必须替换为[:space:]
参见 here。
你必须使用str_replace_all(json_file,',\{\"author[^\*]*\],\s*',"")
(即使对我来说没有多大意义......)