public int countVowelsRegex(String str) {
int count = 0;
if (str.length() > 0) {
// Create a pattern that detects vowels.
Pattern vowelPattern = Pattern.compile("[aeiou]");
Matcher vowelMatcher = vowelPattern.matcher(str);
// Look for the next match and if found, add to count and repeat.
while (vowelMatcher.find())
count++;
}
return count;
}
Posted: March 20, 2023
Return to the snippets listing