The variable is (with the numbers substituted with zeroes so as not to post my actual ip adress)
> myIPaddress
[1] "{\"ip\":\"000.00.000.00\"}"
I want it to be formatted so it just has
> myIPaddress
[1] "000.00.000.00"
I've tried a couple variations of gsub but am struggling. I'm trying to first get rid of everything in the beginning, so I have tried:
> myIPaddress <- readLines("https://lnkd.in/drb3YQZV", warn = FALSE) %>%
+ gsub(".*\\0",'')
Error in gsub(., ".*\\0", "") :
invalid regular expression '{"ip":"000.00.000.00"}', reason 'Invalid contents of {}'
In addition: Warning message:
In gsub(., ".*\\0", "") :
TRE pattern compilation error 'Invalid contents of {}'
So I think it's having a problem with the fact that some of the characters are in the brackets {}. So I found that I could use the perl=TRUE addition and then it just removed everything, as shown below.
I personally think the grep and related functions is base R are very confusing and not intuitive. I recommend you use the "stringr" package from the Tidyverse which is much more elegant.
I feel the need to explain the reason for my "mosterd na de maaltijd" answer:
my answer was the first reaction to the question and I answered that question in terms that the OP used.
My answer was set on hold awaiting clearance by an moderator ??
Later I saw the answer by @W4tRb0y that uses str_extract and indeed I think that is a better solution.
Because my answer was waiting for the moderator, I had the opportunity to delete my solution, but being curious why it was being set for moderation I decided not to do that.
Today my solution was admitted but without giving a reason for the delay.
Still curious why my suggestion was put to moderation. Was it because of the use of gsub?
It is an auto-flagging system, most likely triggered this time because of the IP-like address used in the example (my answer was auto-flagged too), as far as I know, auto-flags al cleared by a single person (the forum administrator) so sometimes it takes a while depending on his workload.