I’m trying to write a regular expression in Java that will match a word of n length that has a at least x number of vowels in it.
So far I’ve come up with the following:
// match words that are length 10 and have at least 2 vowels in them
(?=\w{10})(?:[^aeiou\W]*[aeiuo]){2}\w+
This seems to work but also matches words greater than length 10, i.e.:
wildernesses – matches
volatilizations – matches
voiceprint – matches (this should be the only match)
I would like it so that the length=10 constraint is enforced. I suspect that it may have something to do with the fact that I’m adding letters (the vowels) to the length of the string, but I’m not certain. Any help / guidance will be appreciated.
Use word boundaries,
\b, to prevent the match happening halfway through a word:This will match: