remove quotation marks from string at beginning and end only if both are present

StackOverflow https://stackoverflow.com/questions/11718938

  •  23-06-2021
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top