7

我正在尝试从字符串中删除除撇号之外的所有标点符号。这是我的 exastr2 <-

str2 <- "this doesn't not have an apostrophe,.!@#$%^&*()"
gsub("[[:punct:,^\\']]"," ", str2 )
# [1] "this doesn't not have an apostrophe,.!@#$%^&*()"

我究竟做错了什么?

4

3 回答 3

17

“负前瞻断言”可用于在考虑是否为标点字符之前将任何撇号从考虑中移除。

gsub("(?!')[[:punct:]]", "", str2, perl=TRUE)
# [1] "this doesn't not have an apostrophe"
于 2013-03-06T19:09:19.367 回答
1

我不确定您是否可以'按照您的方式指定除正则表达式之外的所有标点符号。我会用否定检查alphanumerics++ 'space

gsub("[^'[:lower:] ]", "", str2) # per Joshua's comment
# [1] "this doesn't not have an apostrophe"
于 2013-03-06T19:01:16.333 回答
1

你可以使用:

str2 <- "this doesn't not have an apostrophe,.!@#$%^&*()"

library(qdap)
strip(str2, apostrophe.remove = FALSE, lower.case = FALSE)
于 2013-03-06T20:58:37.257 回答