简体   繁体   中英

Regex not allowed Capital letter followed by lower case letters followed by number followed by special character

regex need to match the below format

  1. minimum 1 upper case
  2. minimum 1 lower case
  3. minimum 1 number case
  4. minimum 1 special character

not allow More than two identical characters in a row

but we don't want to follow the specific below Patten(Initial cap word, followed by number, followed by special character- (eg,Fall2015!)) means upper case followed by lower case followed by number followed by special character

(?=.{8,24}$)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[_.!@$*=-?#])(([A-Za-z0-9_.!@$*=-?#])\2?(?!\2))

It looks like the following should tick your boxes:

^(?![A-Z][a-z]+\d+[.!@$*=?#-]$|.*(.)\1\1)(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[.!@$*=?#-]).{8,24}$

See the online demo

  • ^ - Start string anchor.
  • (?! - A negative lookahead for:
    • [AZ][az]+\d+[.?@$*=?#-]$ - The literal pattern you want to negate: An uppercase letter, 1+ lowercase letters, 1+ numbers and a special char before the string ends.
    • | - Or:
    • .*(.)\1\1 - Assert position has no following sequence anywhere of three of the same characters: 0+ characters, a 1st capture group and two immediate backreferences to this group.
    • ) - Close negative lookahead.
  • (?=.*[AZ]) - Positive lookahead to assert an uppercase letter in string.
  • (?=.*[az]) - Positive lookahead to assert a lowercase letter in string.
  • (?=.*\d) - Positive lookahead to assert a number in string.
  • (?=.*[.?@$*=?#-]) - Positive lookahead to assert a special character in string.
  • .{8,24} - Any 8-24 characters other than newline.
  • $ - End string anchor.

Try this:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[_.!@$*=?#-])(?!.*(.)\1\1)(?!.*[A-Z][a-z]+\d+[_.!@$*=?#-])[\w.!@$*=?#-]{8,24}$

The key changes are:

  • ^ to anchor the expression to start
  • (?..*(.)\1\1) which prevents tripled chars
  • (?..*[AZ][az]+\d+[_?!@$*=?#-]) to prevent input like “Fall2015!”
  • [\w.?@$*=,#-]{8,24}$ to restrict input to only these chars and only 8-24 length
  • Moving the hyphen to the end of the char class so it is a literal hyphen (not a range)

Note also the introduction of \d as shorthand for [0-9] and \w as shorthand for [a-zA-Z0-9_] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM