0

I need regex

1) [A-Z][a-z][0-9] Must include uppercase & lowercase letters, numbers & special characters (except + and -).

2) Not more than 2 identical characters in a sequence (e.g., AAxx1224!@ or Password@123 or Google#12 is not acceptable).

I have tried this but dont know how to check 2 identical characters.

^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^%*()!&=]).*$
user3747168
  • 43
  • 1
  • 9

1 Answers1

1

You may add an additional (?!.*(.)\1) lookahead check to disallow consecutive characters and replace .* at the end with [^_+]* (or [^-+]* if you meant hyphens) to match any chars but _ (or -) and +:

^(?!.*(.)\1)(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^*()!&=])[^_+]*$
  ^^^^^^^^^^                                                    ^^^^^

The (?!.*(.)\1) lookahead matches any 0+ chars other than line breaks chars and then captures these chars one by one and tries to match the identical char immediately after them (with the \1 backreference). If the pattern is found, the whole match is failed.

Note that [^_+] may also match line breaks, but I guess it is not the problem here. Anyway, you can add \n\r there to avoid matching them, too.

See the regex demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563