1

I'm trying to create a regular expression that validates following conditions

  1. Min * length
  2. Min * lower case
  3. Min * upper case
  4. Min * special characters

I already have this

^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\S+$).{8,}$

^(?=.*[0-9]) // At least 1 digits
(?=.*[a-z]) // At least 1 lower case
(?=.*[A-Z]) // At least 1 upper case
(?=.*[@#$%^&+=]) // At least 1 special character
.{8,}$ // length 8

The problem is that I need to validate at least 3 digits/lower/upper on any place on the string.

What do I have to add to my RE to make it possible? I was trying to use (?=.*[a-z]{3,}) but that only allows consecutive lower case char...

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
Víctor Cardozo
  • 153
  • 2
  • 13
  • https://regex101.com/r/tP0bQ5/1 – Pranav C Balan Jun 06 '16 at 17:03
  • 1
    Allow users to use the [passwords / phrases](https://xkcd.com/936/) they desire. [Don't limit passwords.](http://jayblanchard.net/security_fail_passwords.html) – Jay Blanchard Jun 06 '16 at 17:25
  • Closed while typing answer >< You can work with [negated class](http://www.regular-expressions.info/charclass.html#negated) in between: `^(?=(?:\D*\d){3})` for three digits, `(?=(?:[^a-z]*[a-z]){3})` for three lowers... [finish and test it here](https://regex101.com/r/vR5pE3/2). – bobble bubble Jun 06 '16 at 17:32

2 Answers2

0

There are easier ways to do this than with a regex. For example, min length can be resolved with len(password) >= min_length. Likewise, a minimum number of lower case letters can be resolved with sum(password.count(let) for let in ["a", "b", "c" ... "z"]) >= min_lower_case.

Everyone_Else
  • 3,206
  • 4
  • 32
  • 55
0

You can use the following regex

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=(?:.*[\da-zA-Z]){3})\S{8,}$

Regex explanation here.

Regular expression visualization

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188