Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert punctuation to space

Tags:

string

regex

r

I have a bunch of strings with punctuation in them that I'd like to convert to spaces:

"This is a string. In addition, this is a string (with one more)."

would become:

"This is a string  In addition  this is a string  with one more  "

I can go thru and do this manually with the stringr package (str_replace_all()) one punctuation symbol at a time (, / . / ! / ( / ) / etc. ), but I'm curious if there's a faster way I'd assume using regex's.

Any suggestions?

like image 307
screechOwl Avatar asked Dec 05 '22 15:12

screechOwl


2 Answers

x <- "This is a string. In addition, this is a string (with one more)."
gsub("[[:punct:]]", " ", x)
[1] "This is a string  In addition  this is a string  with one more  "

See ?gsub for doing quick substitutions like this, and ?regex for details on the [[:punct:]] class, i.e.

‘[:punct:]’ Punctuation characters:
      ‘! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { |
      } ~’.
like image 52
mdsumner Avatar answered Dec 07 '22 23:12

mdsumner


have a look at ?regex

library(stringr)
str_replace_all(x, '[[:punct:]]',' ')

"This is a string  In addition  this is a string  with one more  "
like image 43
mnel Avatar answered Dec 07 '22 22:12

mnel