Removing punctuation except for apostrophes AND intra-word dashes in R

r, string, text

Solution

Use character classes

gsub("[^[:alnum:]['-]", " ", db$text)

## "Interested in energy the environment etc Congrats to our new e-board Ben Nathan Jenny and Adam y'all are sure to lead the club in a great direction next year obama swag"

Problem

I know how to separately remove punctuation and keep apostrophes: ``` gsub( "[^[:alnum:]']", " ", db$text ) ``` or how to keep intra-word dashes with the tm package: ``` removePunctuation(db$text, preserve_intra_word_dashes = TRUE) ``` but I cannot find a way to do both at the same time. For example if my original sentence is: ``` "Interested in energy/the environment/etc.? Congrats to our new e-board! Ben, Nathan, Jenny, and Adam, y'all are sure to lead the club in a great direction next year! #obama #swag" ``` I would like it to be: ``` "Interested in energy the environment etc Congrats to our new e-board Ben Nathan Jenny and Adam y'all are sure to lead the club in a great direction next year obama swag" ``` Of course, there will be extra white spaces, but I can remove them later. I will be grateful for your help.

Original source