Can 开发者_C百科someone please give regular expression for a word, which should have:
- length in the range of 6 to 10
- It must be a combination of alphanumeric, numeric and special character (non-alphanumeric character)
So in other words, all characters are allowed, but at least one letter, one digit, and one "something else" character is required?
^(?=.*\p{L})(?=.*\p{N})(?=.*[^\p{L}\p{N}]).{6,10}$
I wouldn't impose a maximum length restriction on a password, though...
Explanation:
^ # 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
You should do this as a sequential series of tests:
- Is it of the right length?
^.{6,10}$
- Does it contain an alphabetic character?
[A-Za-z]
- Does it contain a digit?
[0-9]
- Does it contain a "special character"
[^0-9A-Za-z]
If it passes all four tests, it's ok.
精彩评论