字母,数字和特殊字符的正则表达式

时间:2011-09-22 14:11:58

标签: .net regex special-characters

有人可以为一个单词提供正则表达式,该单词应具有:

  1. 长度在6至10的范围内
  2. 必须是字母数字,数字和特殊字符(非字母数字字符)的组合

2 个答案:

答案 0 :(得分:4)

换句话说,所有字符都是允许的,但至少需要一个字母,一个数字和一个“别的”字符?

^(?=.*\p{L})(?=.*\p{N})(?=.*[^\p{L}\p{N}]).{6,10}$

我不会对密码施加最大长度限制,但是......

<强>解释

^                    # Match start of string
(?=.*\p{L})          # Assert that string contains at least one letter
(?=.*\p{N})          # Assert that string contains at least one digit
(?=.*[^\p{L}\p{N}])  # Assert that string contains at least one other character
.{6,10}              # Match a string of length 6-10
$                    # Match end of string

答案 1 :(得分:0)

您应该将此作为一系列连续测试:

  1. 它的长度合适吗? ^.{6,10}$
  2. 它是否包含字母字符? [A-Za-z]
  3. 是否包含数字? [0-9]
  4. 是否包含“特殊字符”[^0-9A-Za-z]
  5. 如果它通过了所有四项测试,那就没关系。

相关问题