-3

I am trying to write a regular expression for replacing some of the problematic characters in a json file object. Here is the short -DEMO

I am able to write the php code but i am not able to implement it in R with str_replace as it starts with ,{ charcters. Any suggestion on how to improve the code?

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*","")
Arthur
  • 1,208
  • 13
  • 25
user3570187
  • 1,743
  • 3
  • 17
  • 34

1 Answers1

0
  1. You have to escape the "s or use ' as a wrapper instead of "
  2. You have to escape the control characters twice in R, i.e. \\{, \\^ and especially \\\\ because \ is a control character in R! (Every \\ is replaces by \ and passed to a regex parser.
  3. Eventually, \s must be replaced by [:space:]

See here.

You have to use str_replace_all(json_file,',\\{\\"author[^\\*]*\\],\\s*',"") (even if does not make much sense to me...)

Arthur
  • 1,208
  • 13
  • 25
  • `'\\\\,\{"author[^*]*],\\\\[:space:]*'` instead of `"\,{"author[^*]*],\\s*"` – Arthur Nov 21 '15 at 01:40
  • Can you provide the solution? I am little new to this. – user3570187 Nov 21 '15 at 01:42
  • You don't give an excerpt of json_file in your question. And regexs are known to be the hell to read when you did not write them yourself. So I am afraid you have to try. – Arthur Nov 21 '15 at 01:44
  • I tried it didn't go through. Here is what i wrote? `str_replace_all(json_file ,'\\\\,\{"author[^*]*],\\\\[:space:]*',"")` I got the following error Error: '\{' is an unrecognized escape in character string starting "'\\\\,\{" – user3570187 Nov 21 '15 at 01:45
  • The json file is in the demo. I will try to update the question. – user3570187 Nov 21 '15 at 01:46