I’m trying to write a simple regex for my java program to count syllables, but I’m really new to regular expressions so I didn’t write it correctly, and I’m not sure what I did wrong. The regular expression I wrote is:
((a|i|o|u|y)?!(a|i|o|u|y|e)|e?!(a|i|o|u|y|e|\\w)|\\w)
.
(a|i|o|u|y)?!(a|i|o|u|y|e)
is supposed to match all non-e vowels that are not followed immediately by another vowel.
e?!(a|i|o|u|y|e|\\w)
is supposed to match all e’s that are not followed immediately by a vowel or non-alphebetical character.
\\w
is supposed to match all words that aren’t matched in part by one of the other two pieces.
Instead my first two are matching nothing at all and the \w is matching every alphanumeric character, so clearly I’m doing multiple things wrong.
I really appreciate the help, sorry if this question is a bit basic!
The java method itself is here, but I think it’s working fine:
public int countNumberOfSyllables(String textToCountSyllablesIn) {
int syllableCounter = 0;
Pattern regexForSyllables = Pattern.compile(
"((a|i|o|u|y)?!(a|i|o|u|y|e)|e?!(a|i|o|u|y|e|\\w)|\\w)",
Pattern.CASE_INSENSITIVE);
Matcher syllableMatcher = regexForSyllables.matcher(textToCountSyllablesIn);
while (syllableMatcher.find()) {
syllableCounter++;
}
if (syllableCounter == 0) {
syllableCounter++;
}
return syllableCounter;
}
You forgot the parentheses around your lookahead: