import re
regex = re.compile(r"([\n=\|\^]|([^a-z]:|[^:])\b[^:]+:)Example\.jpg")
test_str = ("Rename:\n"
"[[:File:Example.jpg]]\n"
"Example.jpg\n"
"File:Example.jpg\n"
"[[File:Example.jpg|thumb]]\n"
"logo=Example.jpg\n"
"Do not rename:\n"
"Example filename is Example.jpg\n"
"[[:ru:Файл:Example.jpg]]\n"
"[[b:en:File:Example.jpg]]\n"
"[[:c:File:Example.jpg]]\n"
"duplicate=c:File:Example.jpg")
subst = "$1Renamed.jpg"
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