I want to clean up a string that contains escaped quotation marks. I want to remove the escaped quotation marks the end and beginning of a string but keep intact all qoutation marks within the string. What I came up with is the following.
library(stringr)
s1 <- "\"He said:\"Hello\" - some word\""
str_replace_all(s1, "(^\\\")|(\\\"$)", "")
> [1] "He said:\"Hello\" - some word"
What I am struggling with now is that I only want to remove the quotation marks if and only if there is one at the beginning AND at the end. Otherwise not.The following expression falsely removes the leading one.
s2 <- "\"Hello!\" he said"
str_replace_all(s2, "(^\\\")|(\\\"$)", "")
> [1] "Hello!\" he said"
Here my regex should indicate that I only want to remove them in case the whole string is wrapped in escaped quotation marks. How can I do that?
Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') . The replace() method will return a new string with all double quotes removed.
replace() to remove all quotes from a string. Call str. replace(old, new) on a string with the quote character '"' as old and an empty string "" as new to remove all quotes from the string.
replace() to remove single quotes from a string. Call str. replace(old, new) with old as "'" and new as "" to remove all single quotes from the string.
' You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string.
The following regex seems to work on your examples:
s <- c("\"He said:\"Hello\" - some word\"", "\"Hello!\" he said")
The regex uses back-references (\\1
) to return only the string inside the leading quote ^\"
and the trailing quote \"$
:
r <- gsub("^\"(.*)\"$", "\\1", s)
This results in:
cat(r, sep="\n")
He said:"Hello" - some word
"Hello!" he said
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With