4

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

"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())手动执行此操作,一次一个标点符号(,/./!/(/)/等),但我很好奇是否有更快的方法我会假设使用正则表达式.

有什么建议么?

4

2 回答 2

12
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:
      ‘! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { |
      } ~’.
于 2012-07-16T04:39:44.547 回答
4

看一下?regex

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

"This is a string  In addition  this is a string  with one more  "
于 2012-07-16T04:39:13.943 回答