Search code examples
rtext

How to remove the text between the quotation marks?


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!


Solution

  • 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"'