import re
regex = re.compile(r"^(?:(?=.*?\p{N})(?=.*?[\p{S}\p{P} ])(?=.*?\p{Lu})(?=.*?\p{Ll}))[^\p{C}]{8,16}$", flags=re.MULTILINE)
test_str = ("********** VALID **********\n"
"Password1!\n"
"TestPass###231\n"
"My Pass#123~12`1\n"
"!#$Afs1!@(*''\n"
"VDFt35q#@$@\n"
"éA1!@#!@#!\n\n"
"********* INVALID *********\n"
"PASSWORD1!\n"
"password1!\n"
"Password1\n"
"Password\n"
"Passw1!\n"
"ThisIsMySuperLongPassword1!\n"
"Ae! 1")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html