Remove all punctuation except apostrophes in R

x <- "I like %$@to*&, chew;: gum, but don't like|}{[] bubble@#^)( gum!?"
gsub("[^[:alnum:][:space:]']", "", x)

[1] "I like to chew gum but don't like bubble gum"

The above regex is much more straight forward. It replaces everything that’s not alphanumeric signs, space or apostrophe (caret symbol!) with an empty string.

Leave a Comment