结合两个条件:元音和 space
Combining two conditions: Vowel and a space
我正在尝试检查两个条件,
- 字符串应包含元音。
- 字符串应包含 space.
这是我正在写的内容:
$reg = "/(?=.*(\s)) (?=.*(a|e|i|o|u))/";
但是 运行:
if ( preg_match($reg,"kka "))
echo "YES.";
else
echo "NO.";
我收到 NO
。我做错了什么?
((?:.*)[aeiouAEIOU]+(?:.*)[ ]+(?:.*))|(?:.*)[ ]+((?:.*)[aeiouAEIOU]+(?:.*))
你可以试试这个
以下是使用前瞻的正确方法:
^((?=.*\s.*).)((?=.*[aeiou].*).).*$
此处演示:
如果您想要一个不涉及使用正则表达式的选项,那就是从输入字符串中删除 spaces/vowels 并验证生成的长度是否已减少。
$input = "kka ";
if (strlen(preg_replace("/\s/", "", $input)) < strlen($input) &&
strlen(preg_replace("/[aeiouAEIOU]/", "", $input)) < strlen($input)) {
echo "both conditions satisfied"
else {
echo "both conditions not satisfied"
}
使用preg_replace
和strpos
函数的替代解决方案:
$str = " aa k";
if (($replaced = preg_replace("/[^aeiou ]/i", "", $str)) && strlen($replaced) >= 2
&& strpos($replaced, " ") !== false) {
echo 'Yes';
} else {
echo 'No';
}
我正在尝试检查两个条件,
- 字符串应包含元音。
- 字符串应包含 space.
这是我正在写的内容:
$reg = "/(?=.*(\s)) (?=.*(a|e|i|o|u))/";
但是 运行:
if ( preg_match($reg,"kka "))
echo "YES.";
else
echo "NO.";
我收到 NO
。我做错了什么?
((?:.*)[aeiouAEIOU]+(?:.*)[ ]+(?:.*))|(?:.*)[ ]+((?:.*)[aeiouAEIOU]+(?:.*))
你可以试试这个
以下是使用前瞻的正确方法:
^((?=.*\s.*).)((?=.*[aeiou].*).).*$
此处演示:
如果您想要一个不涉及使用正则表达式的选项,那就是从输入字符串中删除 spaces/vowels 并验证生成的长度是否已减少。
$input = "kka ";
if (strlen(preg_replace("/\s/", "", $input)) < strlen($input) &&
strlen(preg_replace("/[aeiouAEIOU]/", "", $input)) < strlen($input)) {
echo "both conditions satisfied"
else {
echo "both conditions not satisfied"
}
使用preg_replace
和strpos
函数的替代解决方案:
$str = " aa k";
if (($replaced = preg_replace("/[^aeiou ]/i", "", $str)) && strlen($replaced) >= 2
&& strpos($replaced, " ") !== false) {
echo 'Yes';
} else {
echo 'No';
}