Surinder
Surinder

Reputation: 425

Regex to get sorted vowels

Hi I am working on a piece of code, which opens a file that contains some random words like this:

semiconventional
superdeclamatory
semimathematical
semigeometric
stoloniferously
subacademical
supermathematical

Code as follows:

$handle = fopen($filename, "r");
$contents = fread($handle,filesize($filename));
$contentsArray = explode("\n",$contents);
$length = count($contentsArray);

echo "<pre>";

foreach ($contentsArray as $word) {
    if(preg_match("/(?=([aeiou]){3}([^aeiou])$)/", $word, $matches)){
        print_r($word);
        echo "\n";
    }
}

This code is printing all words which,

My requirement is to get only those words which

Upvotes: 1

Views: 926

Answers (2)

Aran-Fey
Aran-Fey

Reputation: 43246

Here's a regex, just because:

^(?=(?:.*?[aeiou]){3})(?!.*u.*[aeio])(?!.*o.*[aei])(?!.*i.*[ae])(?!.*e.*a).*[^aeiou]$

regex101 demo.


Explanation:

^ # start of string anchor
(?= # make sure there are (at least) 3 vowels:
    (?:
        .*? # match any text,...
        [aeiou] #... and a vowel
    ){3} # 3 times
)
(?! # make sure there is NO occurence of
    .*u # a "u" character
    .*[aeio] # followed by an "a", "e", "i" or "o" character
)
(?!.*o.*[aei]) # similarly, make sure there's no "a", "e" or "i" after an "o"
(?!.*i.*[ae]) #... no "a" or "e" after an "i"...
(?!.*e.*a) #... and no "a" after an "e"
.*[^aeiou]$ # finally, make sure the last character is not a vowel.

Upvotes: 2

tripleee
tripleee

Reputation: 189618

The following forces monotonous vowel progression, but doesn't require any vowels at all, and will match anything ending in a consonant within those constraints.

^([^aeiou]*a)*([^aeiou]*e)*([^aeiou]*i)*([^aeiou]*o)*([^aeiou]*u)*[^aeiou]+$

You could augment it with a lookahead assertion at the beginning to force 3 or more vowels. Here is a regex which does that: it specifies three repetitions of vowels, interspersed by optional non-vowels:

([^aeiou]*[aeiou]){3}

Combining the two, we obtain

^(?=([^aeiou]*[aeiou]){3})([^aeiou]*a)*([^aeiou]*e)*([^aeiou]*i)*([^aeiou]*o)*([^aeiou]*u)*[^aeiou]+$

Upvotes: 0

Related Questions