Given a string i want to extract jsons within that string.
Very similar to this Question: Find JSON strings in a string string. Just for R.
Basically, i Need to take the regex and Escape characters if necessary. Therefore, i looked into: Is there an R function to escape a string for regex characters.
What i tried:
txt <- "asdd {a:b, c:d} asdasd"
library(stringr)
quotemeta <- function(string) {
str_replace_all(string, "(\\W)", "\\\\\\1")
}
quotemeta("\{(?:[^{}]|(?R))*\}")
str_extract_all(string = txt, pattern = quotemeta("\\{(?:[^{}]|(?R))*\\}"))
str_extract_all(string = txt, pattern = "\\{\\(\\?\\:\\[\\^\\{\\}\\]\\|\\(\\?R\\)\\)\\*\\}")
str_extract_all(string = txt, pattern = "\\\\{\\(\\?\\:\\[\\^\\{\\}\\]\\|\\(\\?R\\)\\)\\*\\\\}")