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.