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

时间:2015-01-01 09:51:15

标签: php regex

您好我正在处理一段代码,该代码会打开一个包含一些随机单词的文件:

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

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

  • 至少有3个元音(a,e,i,o,u)
  • 不以元音结尾(a,e,i,o,u)

我的要求是只获得那些

的单词
  • 不以元音结尾(a,e,i,o,u)
  • 至少有3个元音(即a,e,i,o,u)不必是唯一的, 但必须按字典顺序排列(即第二个元音等于或来自字母表中的第一个元音。 对于 例如,钻石不符合条件,即使它至少有3个元音因为第三个元音 字母“a”在字母表中第二个字母“i”之前按字典顺序排列。但是,宣泄 因为元音是“a”,“a”,“i”,并且它们在字典顺序中是有资格的 它们出现在单词中的顺序。

2 个答案:

答案 0 :(得分:2)

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

^(?=(?:.*?[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.

答案 1 :(得分:0)

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

^([^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]+$