find the longest consecutive set of vowels in a string

Viewed 247

Trying to find the longest consecutive set of vowels in a string.

I have a very long string.

random_letters <- readr::read_lines("https://byuistats.github.io/M335/data/randomletters.txt")

I have stripped the string of all spaces and periods.

random_letters %>% str_replace_all(., fixed(" "), "") %>% str_replace_all('\\.', '')

I am now trying to find every single time a vowel, or combination of vowels occurs, and then identify the longest one.

so if the string looked like

string <- c("abcnduakngoaibhui")

the output would be

"a" "ua" "oai" "ui
1 Answers

In base R you could use strsplit:

 strsplit(string, '[^aeiou]+')
[[1]]
[1] "a"   "ua"  "oai" "ui" 

Also You could use str_extract_all from stringr package:

stringr::str_extract_all(string, "[aeiou]+")
[[1]]
[1] "a"   "ua"  "oai" "ui" 

These gives you a list, since they are vectorized meaning that the string can be a vector:

string <- c("abcnduakngoaibhui", "aeityuioaiii")
strsplit(string, '[^aeiou]+')
[[1]]
[1] "a"   "ua"  "oai" "ui" 

[[2]]
[1] "aei"     "uioaiii"
Related