某些条件需要正则表达式

时间:2014-02-12 10:42:07

标签: php jquery regex

如何使用以下条件创建正则表达式?

  1. MIN PASSWORD LENGTH:8
  2. MIN MIN OF OF CHARACTERS:1
  3. MIN MINBER OF DIGITS:1
  4. 最少特殊字符数:1
  5. MAX重复字符:2
  6. MAX ALPHABETICAL ORDER:2
  7. MAX NUMERICAL ORDER:2
  8. 尝试:

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

1 个答案:

答案 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
}

如果您还想按降序排列字母/数字(或像zab901这样的包装),则需要将它们添加到替换中。