R从字符串中删除非字母数字符号
我有一个字符串,我想删除所有非字母数字符号,然后放入一个向量。因此:R从字符串中删除非字母数字符号,r,regex,R,Regex,我有一个字符串,我想删除所有非字母数字符号,然后放入一个向量。因此: "This is a string. In addition, this is a string!" 将成为: >stringVector1 "This","is","a","string","In","addition","this","is","a","string" 我已经查看了grep(),但找不到匹配的示例。有什么建议吗?下面是一个例子: > str <- "This is a strin
"This is a string. In addition, this is a string!"
将成为:
>stringVector1
"This","is","a","string","In","addition","this","is","a","string"
我已经查看了grep()
,但找不到匹配的示例。有什么建议吗?下面是一个例子:
> str <- "This is a string. In addition, this is a string!"
> str
[1] "This is a string. In addition, this is a string!"
> strsplit(gsub("[^[:alnum:] ]", "", str), " +")[[1]]
[1] "This" "is" "a" "string" "In" "addition" "this" "is" "a"
[10] "string"
>str
[1] “这是一个字符串。此外,这是一个字符串!”
>strsplit(gsub(“[^[:alnum:][]”,“”,str),“+”[[1]]
[1] “此”“是”“添加”“中的”“字符串”“此”“是”“a”
[10] “字符串”
处理此问题的另一种方法
library(stringr)
text = c("This is a string. In addition, this is a string!")
str_split(str_squish((str_replace_all(text, regex("\\W+"), " "))), " ")
#[1] "This" "is" "a" "string" "In" "addition" "this" "is" "a" "string"
:查找非单词字符并替换str\u replace\u all(text,regex(“\\W+”)”)
”
:减少字符串中重复的空白str_squish()
:将字符串拆分为几段str\u split()
gsub(“[^[:alnum:=\\.]”,“,”哦,诸如此类诸如此类。请安静!=0.42”)
中使用正则表达式,这比多次使用gsub()
函数将每个标点符号替换为“
要好得多。