Home > Software engineering >  Find words with at most two vowels
Find words with at most two vowels

Time:12-12

I have made a function to search words which have two vowels together. The function give me the vowels together (for example: ee, ou), but I need the complete word (tree, four).

lt <- c("tree", "four", "hello", "play")

vowel <- function(x) {  
  a<- regexpr(pattern = "[aeiou]{2}", x) 
    regmatches(x, a)
}

vowel(lt)

# [1] "ee" "ou"

Thank you in advance

CodePudding user response:

We can simply use grepl which in my opinion is more user friendly.

vowel <- function(x) 
   
 {  
   a<- grepl("[aeiou]{2}", x) 
   x[a]
 }
 vowel(lt)
[1] "tree" "four"

CodePudding user response:

Another way is to replace

[aeiou]{2}

with

\w*[aeiou]{2}\w*

to include the letters before and after the double vowel in the word.

CodePudding user response:

Here is solution with stringr:

library(stringr)

lt[str_count(lt, '[^aeiou]') ==2]
[1] "tree" "four"
  • Related