正则表达式得到排序的元音

Regex to get sorted vowels

你好,我正在编写一段代码,它会打开一个包含一些随机单词的文件,如下所示:

semiconventional
superdeclamatory
semimathematical
semigeometric
stoloniferously
subacademical
supermathematical

代码如下:

$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";
    }
}

此代码正在打印所有单词,

我的要求是只获取那些

以下强制单调元音进行,但根本不需要任何元音,并且将匹配在这些限制内以辅音结尾的任何内容。

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

您可以在开头使用前瞻断言来增强它,以强制使用 3 个或更多元音。这是一个执行此操作的正则表达式:它指定三个重复的元音,中间穿插可选的非元音:

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

结合两者,我们得到

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

这是一个正则表达式,只是因为:

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

regex101 demo.


解释:

^ # 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.