-1

I'm writing a program which returns a certain message depending on the strength of the inputted password. In order to be labelled as ''strong'', it has to meet certain requirements, which I'm going to list below in the program's documentation :

#! python3
# strongPassword.py

# Strong Password Detection
# Write a function that uses regular expressions to make sure
# the password string it is passed is strong. A strong password
# is defined as one that is at least eight characters long,
# contains both uppercase and lowercase characters, and has
# at least one digit. You may need to test the string against
# multiple regex patterns to validate its strength.

import pyperclip, re

passwordRegex = re.compile(r'''(
    ^(?=.*[A-Z].*[A-Z])                # at least two capital letters
    (?=.*[!@#$&*])                     # at least one of these special characters
    (?=.*[0-9].*[0-9])                 # at least two numeric digits
    (?=.*[a-z].*[a-z].*[a-z])          # at least three lower case letters
    .{10,}                              # at least 10 total digits
    $
    )''', re.VERBOSE)

def userInputPasswordCheck():
    ppass = input("Enter a potential password: ")
    mo = passwordRegex.search(ppass)
    if (not mo):
        print("Not strong, bling blong")
        return False
    else:
        print("Long, Strong, and down to get the crypto on")
        return True


userInputPasswordCheck()  

I found this code on Github, but I don't quite understand how it manages to create a regular expression that doesn't list certain parts of the pattern in order.

What my concrete question is, is how am I able to write a regex in a more ''vague'' manner ( only mentioning the requirements, like minimum x number of upper case characters, y number of lower case characters and z number of digits, without highlighting the order in which each part has to occur ).

This particular code seems to use ''?='', which I don't fully understand ( lookahead assertion, matching the particular part of the string only if it's followed by another specific part of the string, I know, I just don't understand how it's utilized in this particular code ).

I'd greatly appreciate any assistance.

Thanks in advance.

WilliamFrog8
  • 77
  • 1
  • 10

1 Answers1

0

Here is how:

from re import search

def userInputPasswordCheck():
    pas = input('Input your password: ')
    if all([len(pas) > 7, search('[0-9]', pas), search('[a-z]', pas), search('[A-Z]',pas)]):
        print("Long, Strong, and down to get the crypto on")
        return True
    else:
        print("Not strong, bling blong")
        return False

userInputPasswordCheck()  

Output:

Input your password: Hello101
Long, Strong, and down to get the crypto on
Red
  • 26,798
  • 7
  • 36
  • 58
  • 1
    You're right, fixed it. – Red Jun 30 '20 at 22:49
  • I don't really understand how you're using search without a regex object preceding it ( regex.search(r'Given string') ) and why it has 2 arguments. This doesn't look like a regular expression yet it works line one ? – WilliamFrog8 Jul 02 '20 at 07:52