I am trying to extract all words containing two adjacent vowels in this given string.
x <- "The team sat next to each other all year and still failed."
The results would be "team", "each", "year", "failed"
So far I have tried using [aeiou][aeiou] to do this with regmatches but it only gives me part of the word.
Thanks.
You can place \w* before and after the character class to match "zero or more" word characters.
x <- "The team sat next to each other all year and still failed."
regmatches(x, gregexpr('\\w*[aeiou]{2}\\w*', x))[[1]]
# [1] "team" "each" "year" "failed"
words <-unlist(strsplit(x, " "))
words[grepl("[aeiou]{2}", words)]
#[1] "team" "each" "year" "failed."
If you wanted to clean up the punctuatin it could be:
> words <-unlist(strsplit(x, "[[:punct:] ]"))
> words[grepl("[aeiou]{2}", words)]
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