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

あなたが使用することができます:

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

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

'あなたが行った方法で正規表現内を除いてすべての句読点を指定できるかどうかはわかりません。alphanumerics+ '+spaceを否定でチェックします。

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