1

First time I post something here, so I apologize if I'll do something wrong. I tried already looking for the answer in existing topics, but I was not able to find something that was working 100% for my case.

I basically need to build a python RegEx that will match a password with the following requirements:

  • At least 1 upper case character
  • At least 1 lower case character
  • At least 1 digit character
  • At least 1 special character between the following !"£$%^&?
  • Spaces cannot be included in the password

The minimum length should be 10 characters, the maximum should be 20.

In the following examples:

  • Password!123
  • Password!123 test test test

I would expect to match "Password!123" in both examples, however in the second example the match should stop ad the "3", the "test" (or part of it) should not be included in the match.

So far I built the following:

\b(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!"£$%^&]).{10,20}

But, according to regexr.com , If I test the following: Password!123 test test test

Then I get this match

Password!123 test test test

While I would like to have the following

Password!123 test test test

Any idea on how I could solve this?

Thanks in advance to anyone that will help!!!

KR

Adriano

splash58
  • 26,043
  • 3
  • 22
  • 34
  • Instead of `.{10,20}` use `\S{10,20}`. Note that the starting `\b` will cause `!Password123` to fail. – Sebastian Proske Jan 18 '18 at 14:53
  • See [this](https://stackoverflow.com/questions/48214008/regex-to-include-latin-characters/48214335#48214335) recent post I answered about password validation using regex. – ctwheels Jan 18 '18 at 14:59
  • Also, what language are you using? Each language implements regex differently – ctwheels Jan 18 '18 at 15:06
  • Use [`^(?=\P{Ll}*\p{Ll})(?=\P{Lu}*\p{Lu})(?=\P{N}*\p{N})(?=.*[^\p{L}\p{N}\p{C}])\S{8,}$`](https://regex101.com/r/PLwJEP/5) – ctwheels Jan 18 '18 at 15:11
  • Thanks guys, this one seems to work perfectly: (?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!?])\S{10,20} the language is python, it was stated in the title and in the request :) – Adriano Padovani Jan 18 '18 at 15:30
  • No that last one doesn't work, it matches "LongPassword 123 ! test", because the four parentheses can find a letter, number or symbol anywhere, even if they have to look beyond spaces to do that. To do what you mean, replace all dots by \S – Eily Jan 18 '18 at 16:08
  • I see, you're right, thanks. – Adriano Padovani Jan 19 '18 at 13:12
  • Possible duplicate of [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation) – ctwheels Jan 19 '18 at 18:21

1 Answers1

1

This regex seems to work:

(?=\S{10,})(?=\S*[A-Z])(?=\S*[a-z])(?=\S*[0-9])(?=\S*[!£$%^&\"])(?<!\S)\S{10,20}(?=\s|\Z)

A test can be found here

LukStorms
  • 28,916
  • 5
  • 31
  • 45