user899055
user899055

Reputation: 301

Regular expression for Alphabet, numeric and special character

Can someone please give regular expression for a word, which should have:

  1. length in the range of 6 to 10
  2. It must be a combination of alphanumeric, numeric and special character (non-alphanumeric character)

Upvotes: 0

Views: 6111

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

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

Upvotes: 5

Kusalananda
Kusalananda

Reputation: 15603

You should do this as a sequential series of tests:

  1. Is it of the right length? ^.{6,10}$
  2. Does it contain an alphabetic character? [A-Za-z]
  3. Does it contain a digit? [0-9]
  4. Does it contain a "special character" [^0-9A-Za-z]

If it passes all four tests, it's ok.

Upvotes: 0

Related Questions