How to remove regular expressions in a string in R?

String "\r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\tLocation\r\n\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\tSan Francisco, CA;Oakland, CA" and I want it to appear as "Location","San Francisco, CA;Oakland, CA" .

Is there a function for this? Thanks!

+4
source share
2 answers

You can use regex to remove special characters:

 x <-"\r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\tLocation\r\n\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\tSan Francisco, CA;Oakland, CA" gsub('[\r\n\t]', '', x) 

However, if you really have a line with all these \ , the answer becomes something more similar:

 gsub('\\\\[az]', '', x) 
+6
source

It is not clear whether you want to separate the two parts of the string or not. If you have done this, you can use:

 test <- c("\r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\tLocation\r\n\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\tSan Francisco, CA;Oakland, CA") result <- unlist(strsplit(test,"\\r|\\n|\\t")) result <- result[result!=""] result [1] "Location" "San Francisco, CA;Oakland, CA" 
+2
source

All Articles