Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove punctuation from text (except the symbol &)

Tags:

string

regex

r

I need to remove punctuation from the text:

 data <- "Type the command AT&W enter. in order to save the new protocol on modem;"
 gsub('[[:punct:] ]+',' ',data)

This solution gives the result

[1] "Type the command AT W enter in order to save the new protocol on modem "

This is not the desired result because I would like to save &, hence:

[1] "Type the command AT&W enter in order to save the new protocol on modem "
like image 940
Mark Avatar asked Feb 24 '26 21:02

Mark


1 Answers

You could try a user defined regex consisting of anything that is not an $ or an alpha numeric:

data <- "Type the command AT&W enter. in order to save the new protocol on modem;"

gsub('[^&[:alnum:] ]+',' ',data)
like image 158
greg5678 Avatar answered Feb 26 '26 12:02

greg5678