简体   繁体   中英

Replace a pattern with special characters in R

I have a string x as below. I am trying to replace "c("" and "\nLOC" such that I am left with only Abc,xyz.

x<-"c(\"Abc, xyz\\nLOC"

This is what I tried which works but is there a shorter way of doing it?

x <-str_replace_all(x, "[^[:alnum:]]", " ")
x <-str_replace_all(x, "c  ", "")
x <-str_replace_all(x, "nLOC", "")

With only a single example, it's hard to know what to generalize... You could do it all in one big pattern,

str_replace_all(x, "[^[:alnum:],]|^c|nLOC", "")
[1] "Abc,xyz"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM