I'm working on MOOC Java II problem 10.15:
Create the method public boolean allVowels(String string) that uses a regular expression to check whether all the characters in the parameter string are vowels.
The question wants us to detect any strings that have one or more consonants.
This is my method:
public boolean allVowels(String string){
String regex = "[b-df-hj-np-tv-z]";
if(string.matches(regex)){
return false;
}
return true;
}
Why does this method fail to detect a string that has a consonant mixed with a vowel e.g. "ab"?
I thought that [b-df-hj-np-tv-z]
means "return a match if the string contains any of these characters in this list"... so why does a string like "ab"
fail to return false
in my method?
Keep in mind my regex pattern does return a match if I use something like regex101.com e.g. enter image description here
I've tried playing around with the regex pattern, but I can't seem to pass the MOOC test case... not sure what I'm doing wrong :-(