I am using Python to demonstrate the REGEX example using Python's re regex module to validate the password. :
Included in my solution, inserted in the regex pattern are the the old_password, username, and the word "password" for different letter case variations.
The solution matches with the requirements for the password:
PYTHON CODE:
import re
old_password = "Matilda#43555"
// # Collect username and password (defaults "matilda55577" and "HelloSunshine!456" respectively):
username = input("Username: ") or "matilda55577"
new_password = input("Password: ") or "HelloSunshine!456"
// # Insert username and password in the regex pattern:
pattern = f"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#?!#@^*~+])(?!.*[pP][aA][sS][sS][wW][oO][rR][dD])(?!.*\.\w)(?!.*{username})(?!{old_password}$)" + ".{8,}$"
print(pattern)
pattern_re = re.compile(pattern)
for item in test_password_list:
print(item, end=": ")
if pattern_re.match(item) == None:
print("(---NOT VALID)")
else:
print("(---VALID)")
NOTES REGEX PATTERN:
pattern = f"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#?!#@^*~+])(?!.*[pP][aA][sS][sS][wW][oO][rR][dD])(?!.*\.\w)(?!.*{username})(?!{old_password}$)" + ".{8,}$"
ELEMENTS:
f("...{username}{password}")
In Python, we can use the f-string and {variable_name_here}
to enter the username
and old_password
values in the pattern.
^...$
Pattern must match from beginning ^
to end $
.
(?=...)
Positive lookahead is successful if can match to the right. Will not consume characters.
(?!...)
Negative lookahead is successful if can NOT match to the right. Will not consume characters.
.
Dot (.
) special character. Matches all characters (except newline character \n
unless single line flag is on)
.*
Matches 0 or more characters, as many as possible to make a match (greedy).
\d
Matches a number from zero to 9.
[...]
Character class. Matches any one character listed inside the character class.
[a-z]
Range of letters FROM a - z
TO. Matches one lower case letter.
[A-Z]
Range of letters FROM A - Z
TO. Matches one upper case letter.
[#?!#@^*~+]
Matches any one character listed in the character class, in this case matches one of "#?!#@^*~+".
PATTERN STATEMENTS:
Between the beginning ^
and the end of $
string:
From the beginning of the string:
(?=.*\d)
Lookahead and make sure there is at least one number.
(?=.*[a-z])
Lookahead and make sure there is at least one lower case letter.
(?=.*[A-Z])
Lookahead and make sure there is at least one upper case letter.
(?=.*[#?!#@^*~+])
Lookahead and make sure there is at least one of these characters # ? ! # @ ^ * ~ +
.
(?!.*[pP][aA][sS][sS][wW][oO][rR][dD])
Negative lookahead to and make sure that the word "password" is not present in any combination of upper and lower case letters.
(?!.*\.\w)
Negative lookahead to make sure that there are no literal dots/periods (.
) followed by a letter (rules out possible email or url)
(?!.*{username})
Fill in username
. Negative lookahead to make sure that the username is not any part of the string.
(?!{old_password}$)
Fill in old_password
. Negative lookahead to make sure that the new_password does NOT match the old_password exactly. Note $
says it has reached the end of string.
.{8,}
If all preceding lookaheads match, proceed to capture 8
or more characters. If less than 8
characters. No match, i.e. invalid new password.
$
End of string. If you get here, you have a valid password.
REGEX DEMO: https://regex101.com/r/XlyJNL/2
LIST OF TEST PASSWORDS:
test_password_list = [
new_password,
old_password,
username,
"Mother#234!",
"aaahelma!345",
"aaahElma!345",
"oldWorld#2222",
"77#elloYello!!!",
"Matilda#43555555",
"111matilda55577OK"
"1123444A!!!!!!!!",
"1123444A!!!!!Park!!",
"aaapasSword123#!KC",
"pASsWORd123#"
"4Matilda#43555234GGG",
"matilda55577!EFR444",
"maTmatilda55577!EFR444",
"hello.www.com.Park1!.com.youtube.com",
"https://stackoverflow.com/question/1234A!",
"ITs!stackoverflow123.com",
"ToSh4!",
"To2!",
"2!sH1",
"[email protected]",
"Matilda#43555",
"Matilda#435555"
]
RESULT:
Username:
Password:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#?!#@^*~+])(?!.*[pP][aA][sS][sS][wW][oO][rR][dD])(?!.*\.\w)(?!.*matilda55577)(?!Matilda#43555$).{8,}$
HelloSunshine!456: (---VALID)
Matilda#43555: (---NOT VALID)
matilda55577: (---NOT VALID)
Mother#234!: (---VALID)
aaahelma!345: (---NOT VALID)
aaahElma!345: (---VALID)
oldWorld#2222: (---VALID)
77#elloYello!!!: (---VALID)
Matilda#43555555: (---VALID)
111matilda55577OK1123444A!!!!!!!!: (---NOT VALID)
1123444A!!!!!Park!!: (---VALID)
aaapasSword123#!KC: (---NOT VALID)
pASsWORd123#4Matilda#43555234GGG: (---NOT VALID)
matilda55577!EFR444: (---NOT VALID)
maTmatilda55577!EFR444: (---NOT VALID)
hello.www.com.Park1!.com.youtube.com: (---NOT VALID)
https://stackoverflow.com/question/1234A!: (---NOT VALID)
ITs!stackoverflow123.com: (---NOT VALID)
ToSh4!: (---NOT VALID)
To2!: (---NOT VALID)
2!sH1: (---NOT VALID)
[email protected]: (---NOT VALID)
Matilda#43555: (---NOT VALID)
Matilda#435555: (---VALID)