我正在尝试从字符串中删除所有标点符号,但保留撇号。以下是我的exastr2 -
str2 <- "this doesn't not have an apostrophe,.!@#$%^&*()"
gsub("[[:punct:,^\\']]"," ", str2 )
# [1] "this doesn't not have an apostrophe,.!@#$%^&*()"
我做错了什么?
我正在尝试从字符串中删除所有标点符号,但保留撇号。以下是我的exastr2 -
str2 <- "this doesn't not have an apostrophe,.!@#$%^&*()"
gsub("[[:punct:,^\\']]"," ", str2 )
# [1] "this doesn't not have an apostrophe,.!@#$%^&*()"
负向先行断言"可用于排除任何撇号,甚至在测试其是否为标点符号之前。
"gsub("(?!')[[:punct:]]", "", str2, perl=TRUE)
# [1] "this doesn't not have an apostrophe"
'
之外的所有标点符号。我会使用否定来检查字母数字
+'
+空格
:gsub("[^'[:lower:] ]", "", str2) # per Joshua's comment
# [1] "this doesn't not have an apostrophe"
[[:lower:]]
而不是[a-z]
,因为后者是与语言环境相关的。 - Joshua Ulrich
str2 <- "this doesn't not have an apostrophe,.!@#$%^&*()"
library(qdap)
strip(str2, apostrophe.remove = FALSE, lower.case = FALSE)
qdap
”。有什么想法吗? - Arun