Using the strsplit method for @FrankieTheKneeMan:
x <- c("I like \\the big\\ red \\dog\\ $\\hat + \\bar$, here it is $\\bar{x}$",
"I have $50 to \\spend\\",
"$\\frac{4}{5}$ is nice",
"$\\30\\ is nice too")
bits <- strsplit(paste(x, ''), '$', fixed=T)
out <- sapply(bits, function (x) {
idx <- unique(c(seq(1, length(x), by=2), length(x)))
x[idx] <- gsub('\\', '\"', x[idx], fixed=T)
x <- paste(x, collapse='$')
substring(x, 1, nchar(x) - 1)
}, USE.NAMES=F)
This will always have cases in which it fails ( "I have $20. \\hi\\ Now I have $30"), so you have to keep that in mind and test it against other lines in the format you expect.
source
share