I'm trying to remove all punctuation from a string except apostrophes. Here's my exastr2 <-
str2 <- "this doesn't not have an apostrophe,.!@#$%^&*()"
gsub("[[:punct:,^\\']]"," ", str2 )
# [1] "this doesn't not have an apostrophe,.!@#$%^&*()"
What am I doing wrong?
A "negative lookahead assertion" can be used to remove from consideration any apostrophes, before they are even tested for being punctuation characters.
gsub("(?!')[[:punct:]]", "", str2, perl=TRUE)
# [1] "this doesn't not have an apostrophe"
I am not sure if you can specify all punctuations except '
within a regexp the way you've done. I would check for alphanumerics
+ '
+ space
with negation:
gsub("[^'[:lower:] ]", "", str2) # per Joshua's comment
# [1] "this doesn't not have an apostrophe"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With