正则表达式得到排序的元音
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";
}
}
此代码正在打印所有单词,
- 至少有 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”,并且它们在
它们在单词中出现的顺序。
以下强制单调元音进行,但根本不需要任何元音,并且将匹配在这些限制内以辅音结尾的任何内容。
^([^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]$
解释:
^ # 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.
你好,我正在编写一段代码,它会打开一个包含一些随机单词的文件,如下所示:
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”,并且它们在 它们在单词中出现的顺序。
以下强制单调元音进行,但根本不需要任何元音,并且将匹配在这些限制内以辅音结尾的任何内容。
^([^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]$
解释:
^ # 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.