Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

r

stringr

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?

like image 985
Mark Heckmann Avatar asked Jul 30 '12 09:07

Mark Heckmann


People also ask

How do you remove double quotes from the beginning and end of a string?

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.

How do you remove double quotes from the beginning and end of a string in Python?

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.

How do I remove single quotes from the beginning and end of a string in Python?

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.

How do you escape quotation marks in a 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.


1 Answers

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
like image 61
Andrie Avatar answered Oct 03 '22 22:10

Andrie