如何使用以下条件创建正则表达式?
尝试:
^(?=(.*\d){1})(?=.*[!@#$%_-]) (?=.*[0-9a-zA-Z]){1,2}.{8,}$
答案 0 :(得分:2)
你可以这样做(我用字母/数字表示你的意思是ASCII字母/数字),但你的最后两个要求并不简单:
if (preg_match(
'/^ # Start of string
(?=.*[a-z]) # Assert at least one letter
(?=.*[0-9]) # and one digit
(?=.*[^a-z0-9]) # and one "other" character
(?!.*(.)\1{2}) # and no three identical characters in a row
(?!.*(?:abc|bcd|cde|def|efg|fgh|ghi|
hij|ijk|jkl|klm|lmn|mno|nop|
opq|pqr|qrs|rst|stu|tuv|uvw|
vwx|wxy|xyz)) # and no three-letter sequence
(?!.*(?:123|234|345|456|567|678|789|890)) # and no three-digit sequence
.{8,} # Match at least 8 characters
$ # End of string/ix',
$subject)) {
# Successful match
} else {
# Match attempt failed
}
如果您还想按降序排列字母/数字(或像zab
或901
这样的包装),则需要将它们添加到替换中。