John
John

Reputation: 43199

Remove everything from string but spaces, dashes and letters in R?

I am trying to remove everything in a string except spaces dashes(-) and letters. For example

string1 <- "test-%432string *#$ one!~+"

how do I return "test-string one"

I tried : gsub("[^a-zA-Z-\s]", "", string1) to no avail -- it removes the space, which should be left.

Thanks for any help.

Upvotes: 2

Views: 1293

Answers (1)

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44720

Try this

/[^\w\-\s]|\d/

That worked for me. You can try it out on rubular.com. Enjoy.

Or in R form:

gsub("[^\\w\\-\\s]|\\d","",string1,perl = TRUE)
[1] "test-string  one"

Upvotes: 7

Related Questions