gpt4 book ai didi

php - 特定条件需要正则表达式模式

转载 作者:行者123 更新时间:2023-12-01 04:10:04 25 4
gpt4 key购买 nike

如何创建具有以下条件的正则表达式?

  1. 最小密码长度:8
  2. 最少字符数:1
  3. 最小位数:1
  4. 特殊字符的最少数量:1
  5. 最大重复字符数:2
  6. 最大字母顺序:2
  7. 最大数字顺序:2

尝试过:

^(?=(.*\d){1})(?=.*[!@#$%_-]) (?=.*[0-9a-zA-Z]){1,2}.{8,}$

最佳答案

可以做到这一点(我假设字母/数字是指 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
}

如果您还想按降序排除字母/数字(或像 zab901 这样的环绕),您需要将它们添加到交替中。

关于php - 特定条件需要正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21725558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com