import re
regex = re.compile(r"""
(<\?=)
(
(
(?!
(htmlspecialchars|htmlentities|json_encode)
)
.
)*
[\$]{1,}
(.*)
)
(\?>)
""", flags=re.IGNORECASE | re.VERBOSE)
test_str = ("<?= $lorem ?>\n"
"<?= _($lorem) ?>\n\n"
"<?= 'ipsumz' ?>\n"
"<?= _('ipsum') ?>\n\n"
"//Conditions ternaires\n"
"<?= ($a == $b) ? 'lorem' : 'ipsum' ?>\n\n"
"//Chaînes contenant un htmlspecialchars()\n"
"<?= 'lorem ' . htmlspecialchars('lorem ipsum') ?>\n\n"
"//Chaînes contenant un htmlentities()\n"
"<?= htmlentities('lorem ipsum') ?>")
subst = "purify($2)"
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