I have the following regex that splits on any space or punctuation. How can I exclude 1 or more punctuation characters from :punct:
? Let's say I'd like to exclude apostrophes and commas. I know I could explicitly use [all punctuation marks in here]
instead of [[:punct:]]
but I'm hoping for an exclusion method.
X <- "I'm not that good at regex yet, but am getting better!"
strsplit(X, "[[:space:]]|(?=[[:punct:]])", perl=TRUE)
[1] "I" "'" "m" "not" "that" "good" "at" "regex" "yet"
[10] "," "" "but" "am" "getting" "better" "!"
It's not clear to me what you want the result to be, but you might be able to use negative classes like this answer.
R> strsplit(X, "[[:space:]]|(?=[^,'[:^punct:]])", perl=TRUE)[[1]]
[1] "I'm" "not" "that" "good" "at" "regex" "yet,"
[8] "but" "am" "getting" "better" "!"
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