您好我正在处理一段代码,该代码会打开一个包含一些随机单词的文件:
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";
}
}
此代码正在打印所有单词,
我的要求是只获得那些
的单词答案 0 :(得分:2)
这是一个正则表达式,只是因为:
^(?=(?:.*?[aeiou]){3})(?!.*u.*[aeio])(?!.*o.*[aei])(?!.*i.*[ae])(?!.*e.*a).*[^aeiou]$
说明:
^ # 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]+$