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!
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"
txt <- 'aaaaa,"bbb",ccccc,"dddd",abcd"dddd"'