我正在尝试从字符串中删除除撇号之外的所有标点符号。这是我的 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"
我不确定您是否可以'
按照您的方式指定除正则表达式之外的所有标点符号。我会用否定检查alphanumerics
++ '
:space
gsub("[^'[:lower:] ]", "", str2) # per Joshua's comment
# [1] "this doesn't not have an apostrophe"
你可以使用:
str2 <- "this doesn't not have an apostrophe,.!@#$%^&*()"
library(qdap)
strip(str2, apostrophe.remove = FALSE, lower.case = FALSE)