Study assignment:

Write a function that uses regular expressions to verify that the passed string is a strong password. Strong passwords are considered to be at least eight characters long, contain upper and lower case characters and include at least one digit.

Fulfilled all the conditions except for selecting a password by length:

[AZ]+[az]+\w{1,} 

Regex101 Example

What do I need to register in order to add a sample condition by password length (at least 8 characters)?

  • Not all. Check, for example, the password absghk4D - Enikeyschik
  • one
    You have no question in question .. - Kromster
  • It seemed clear to me, well, well, he added the question: "What should I register to add a sample condition by the password length (at least 8 characters)?" - Ronin

2 answers 2

I took a little and simplified the regular schedule from this answer :

 import re pattern_password = re.compile(r'^(?=.*[0-9].*)(?=.*[az].*)(?=.*[AZ].*)[0-9a-zA-Z]{8,}$') print(bool(pattern_password.match('absghk4D'))) # True print(bool(pattern_password.match('abc123FF'))) # True print(bool(pattern_password.match('123ABCac'))) # True print(bool(pattern_password.match('abcFF123'))) # True print() print(bool(pattern_password.match('absghk4D $%#$'))) # False print(bool(pattern_password.match(''))) # False print(bool(pattern_password.match('bsghk4D'))) # False print(bool(pattern_password.match('abc_aaFF'))) # False print(bool(pattern_password.match('abcabcac'))) # False print(bool(pattern_password.match('ABCDF!@##'))) # False 

Explanation of the regular season:

  • (?=.*[0-9]) - the string contains at least one number;
  • (?=.*[az]) - the string contains at least one Latin lowercase letter;
  • (?=.*[AZ]) - the string contains at least one Latin uppercase letter;
  • [0-9a-zA-Z]{8,} - the string consists of at least 8 of the above characters.

To add support for special characters you need to include them in the last part of the regular season:

 ...[0-9a-zA-Z$%#^]{8,}$') 

Then and

 print(bool(pattern_password.match('$b#FF123'))) # True 
  • Is abs-ghk4D $%#$ strong password? - Wiktor Stribiżew
  • @ WiktorStribiżew, the problem statement has a list of characters in the password, and there are no special characters in it - gil9red
  • There is no such requirement there. And if so, then why do you have a valid absghk4D $%#$ ? Something is wrong here. - Wiktor Stribiżew
  • @Ronin If you incorrectly validate, you can get the wrong result. - Enikeyschik
  • @ WiktorStribiżew, thanks for the comment, corrected the regular season - gil9red
 import re def test_pwd(pwd): ''' >>> test_pwd("absghk4D") True >>> test_pwd("absg4D") False >>> test_pwd("'\ra\/bsghk4D") Traceback (most recent call last): ... SyntaxError: EOL while scanning string literal ''' def f(exp, word=pwd): return bool(re.search(exp, word)) return f('\d') and f('[AZ]') and f('[az]') and f('[0-9a-zA-Z]{8,}') 

You can also handle exceptions that may occur due to the option entered by the user.

  • one
    \w includes _ - gil9red
  • Where is the answer? Here is just an extract from the documentation. - Ronin
  • one
    corrected to be the answer - Eugene Dennis
  • added to work with "absghk4D" - Eugene Dennis
  • one
    @EugeneDennis, it seems to me that you can use search instead of findall, and immediately return bool: return bool(re.search(...)) . `r '{}'. format (pwd)` looks weird, why not just pwd ? Back in def f(exp, word=pwd): there is exp , but it is not used - gil9red