Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regular expression - find strings with no vowels

Tags:

java

regex

I have a list of words and I have to output the number of words with no vowels in them. I have this so far

String matchString = "[^aeiou]"
for(String s: list) if(s.matches(matchString.toLowerCase())) {
        System.out.println(s);
        numMatches++;
    }

I'm more worried that the reg expression is wrong.

like image 738
user3704986 Avatar asked May 29 '26 04:05

user3704986


1 Answers

Change regex to

[^aeiou]+ 
        ^-- important part

to test if string is build from one or more non vowel characters. Currently you are just checking if stirng is build from one character which is not a e i o u.

You can also make your regex case insensitive by adding (?i) flag at start. This way characters used in regex will represent its lower and upper case

(?i)[^aeiou]+
like image 129
Pshemo Avatar answered May 31 '26 09:05

Pshemo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!