import re
regex = re.compile(r"{% image (([a-z]|[A-Z]|[0-9]|\.|_|=|-|'|\s)*)(?<!loading='lazy')\s* %}", flags=re.MULTILINE)
test_str = ("{% image self.hero_image fill-803x300 as hero_image loading='lazy' %}\n"
"{% image self.hero_image fill-803x300 as hero_image %}\n"
"{% image self.hero_image fill-803x300 as hero_image lo %}\n"
"{% image self.image_desktop fill-480x776.noauto as image_480 %}")
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