Skip to content
Prev 350192 / 398506 Next

regexpr - ignore all special characters and punctuation in a string

You can use the [:alnum:] regex class with gsub.

str1 <- "What a nice day today! - Story of happiness: Part 2."
str2 <- "What a nice day today: Story of happiness (Part 2)"

gsub("[^[:alnum:]]", "", str1) == gsub("[^[:alnum:]]", "", str2)
[1] TRUE

The same can be done with the stringr package if you really are partial to
it.

library(stringr)





On Mon, Apr 20, 2015 at 9:10 AM, Sven E. Templer <sven.templer at gmail.com>
wrote: