view raw
ClownInTheMoon ClownInTheMoon - 7 months ago 34
Java Question

Regex pattern matching behavior for basic implementation

I'm attempting to count the number of syllables in a given word using regex. I've referred to some other posts on stack overflow but am getting strange behavior when I run my program.

Here is what I'm attempting

int n=0;
Pattern p = Pattern.compile("[aeiouy]+[^$e]");
Matcher m = p.matcher("Harry");

while(m.find()) {


Now, it's printing out 1. But there is both an "a" and "y" within the string. Where have I gone wrong?


At the end of the String there are no more letters to match so remove the expression [^$e]

Pattern p = Pattern.compile("[aeiouy]+");

Although from your comment you seem to want to treat e as a special case. You could do

Pattern p = Pattern.compile("[aiouy]|e[^$]");