アポストロフィ以外のすべての句読点を文字列から削除しようとしています。これが私の 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"
あなたが使用することができます:
str2 <- "this doesn't not have an apostrophe,.!@#$%^&*()"
library(qdap)
strip(str2, apostrophe.remove = FALSE, lower.case = FALSE)
'
あなたが行った方法で正規表現内を除いてすべての句読点を指定できるかどうかはわかりません。alphanumerics
+ '
+space
を否定でチェックします。
gsub("[^'[:lower:] ]", "", str2) # per Joshua's comment
# [1] "this doesn't not have an apostrophe"