import re
regex = re.compile(r"""
(?(DEFINE)
(?<next_open_tag>
[^<]*+
(?i: <++[^<?s][^<]*
| <++(?! \?php
| \?=
| script\s*language\s*=\s*([\'"]?)php\g{-1}\s*>
) [^<]*
)*+
(?i: <++(?: \?php
| \?=
| [^>]+
)
| \z
)
)
)
\A (?&next_open_tag) \K
|
[^'"`/#<?]*+
(?: '(?:[^'\\]+|\\.)*+' [^\'"`/#<?]*
| "(?:[^"\\]+|\\.)*+" [^\'"`/#<?]*
| `(?:[^`\\]+|\\.)*+` [^\'"`/#<?]*
| /(?![/*]) [^\'"`/#<?]* # stop for // or /*
| # if close tag ?>
\? (?: >(?&next_open_tag)[^\'"`/#<?]* | )
| < (?: # heredoc or nowdoc
<<[\ \t]*([\'"]?)
([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)
\g{-2}[\ \t]*[\r\n]
(?-s:.*+[\r\n])*?
\g{-1}[\r\n;]
[^\'"`/#<?]*
| (?i: /script\s*>)
(?&next_open_tag)
[^\'"`/#<?]*
| [^\'"`/#<?]*
)
)*+
\K
(?: (?://|\#)(?:[^\n?]+|\?(?!>))*+ # single line comment // и #
| /\*(?:[^*]+|\*(?!/))*+\*/ # multi line comment /* */
)?
""", flags=re.VERBOSE | re.DOTALL)
test_str = ("<!-- *//* -->\n"
"<?php\n\n"
"$a = \"text */*\"; // comment\n"
"$b = 'text *//*'; # comment\n"
"//*\n\n"
"if ($a) {\n"
" echo $b;\n"
"}\n\n"
"/*/\n\n"
"if ($bar) {\n"
" echo $foo;\n"
"}\n\n"
"// */\n\n"
"?>\n"
"<!-- *//* -->\n\n\n"
"<script language = \"php\" >\n\n"
"# comment ?> Text <?php $a = '/*'; // comment\n\n"
"$b = @`ls ./* -al`;\n\n"
"# */\n"
"</script>\n\n"
"<!-- *//* --> \n")
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