import re
regex = re.compile(r"^(?!^([a-z]+|[A-Z]+|\d+|[\W_]+)$).{4,20}$", flags=re.MULTILINE)
test_str = ("aaaaaaaaa\n"
"111111111\n"
"AAAAAAAA\n"
"!@#!@#!@\n"
"aaaaa11111\n"
"aaaaaAAAAA\n"
"aaaaa!@#@!\n"
"11111aaaaa\n"
"11111AAAAA\n"
"11111!@!@$\n"
"AAAAAaaaaa\n"
"AAAAA11111\n"
"AAAAA12312\n"
"!@#$%&aaaa\n"
"!@#$%&1231\n"
"!@#$%&AAAA\n"
"1123as%@#%AAFFSS\n"
"sdffsg#$52ERgWAfW14\n"
"sdffsg#$52ERgWAfW141\n"
"sdffsg#$52ERgWAfW1412\n"
"__________\n"
"----------\n"
"||||||||||\n"
"¹¹¹¹¹¹¹¹¹¹\n"
"²²²²²²²²²²\n"
"³³³³³³³³³\n"
"££££££££\n"
"¢¢¢¢¢¢¢¢¢¢\n"
"¬¬¬¬¬¬¬¬¬¬\n"
"//////////\n"
"°°°°°°°°°°\n"
"ªªªªªªªªªªª\n"
"aaaaaaaaaaaaa\n"
"ºººººººººººººº\n"
"₢₢₢₢₢₢₢₢₢₢₢₢₢₢")
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