How to remove the text between the quotation marks?

Viewed 62

Here is my data:

x <- 'aaaaa,"bbb",ccccc,"dddd",abcd"dddd"'

How to remove: "bbb","dddd" and "dddd"

So I can get:

"aaaaa","ccccc","abcd"

Thanks!

2 Answers

We could use gsub to match the pattern of double quote (") followed by one or more characters that are not a double quote ([^"]+) followed by a double quote (") and a , if present and replace with blank ("")

gsub('"[^"]+",?', "", txt)
[1] "aaaaa,ccccc,abcd"

If we need to keep the , in between

gsub('"[^"]+"', "", txt)
[1] "aaaaa,,ccccc,,abcd"

data

txt <- 'aaaaa,"bbb",ccccc,"dddd",abcd"dddd"'

You can drop the words with double quotes in them.

gsub('"\\w+",?', '', x)
#[1] "aaaaa,ccccc,abcd"

If you want them as vector of words -

strsplit(gsub('"\\w+",?', '', x), ',')[[1]]
#[1] "aaaaa" "ccccc" "abcd" 
Related