这将做的工作:
/^(?=(?:.*[a-z]){2})(?=(?:.*\d){10})[a-z0-9 ]+$/i
在PHP中使用它:
if (preg_match('/^(?=(?:.*[a-z]){2})(?=(?:.*\d){10})[a-z0-9 ]+$/i', $myString)) {
echo "OK!\n";
} else {
echo "Error!\n";
}
说明:
/ : regex delimiter
^ : start of string
(?= : lookahead
(?: : non capture group
.*[a-z] : 0 or more any character followed by a letter
){2} : end of group, must be present twice
) : end of lookahead
(?= : lookahaed
(?: : non capture group
.*\d : 0 or more any character followed by a digit
){10} : end of group, must be present 10 times
) : end of lookahaed
[a-z0-9 ]+ : character class, allowed characters are letters, digit and space
$ : end of string
/i : regex delimiter, case insensitive
你在向前here找到有用的信息,对组here