将标点符号转换为空格

时间:2012-07-16 04:36:09

标签: regex string r

我有一堆带有标点符号的字符串,我想将其转换为空格:

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

会变成:

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

我可以通过stringr包(str_replace_all())一次一个标点符号手动执行此操作(,/。/!/(/)/等),但是我好奇,如果有一种更快的方式我会假设使用正则表达式。

有什么建议吗?

2 个答案:

答案 0 :(得分:10)

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  "

有关?gsub类的详细信息,请参阅?regex进行快速替换,[[:punct:]]

‘[:punct:]’ Punctuation characters:
      ‘! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { |
      } ~’.

答案 1 :(得分:4)

查看?regex

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

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