import re
regex = re.compile(r"(?<!\d|^)(?<!(?<![^\W_])(?:sha|md))(?=\d)|(?<=\d)(?!\d|$)|_", flags=re.MULTILINE)
test_str = ("Aisha256\n"
"ai_sha256\n"
"test123\n"
"84test\n"
"test md5\n"
"sha256\n"
"word two sha1\n"
"w0rd\n"
"test_md5\n"
"sha256\n"
"md5")
subst = " "
result = regex.sub(subst, test_str)
if result:
print(result)
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