我有一堆带有标点符号的字符串,我想将其转换为空格:
"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()
)一次一个标点符号手动执行此操作(,/。/!/(/)/等),但是我好奇,如果有一种更快的方式我会假设使用正则表达式。
有什么建议吗?
答案 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 "