# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = 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 /* */
)?
"""
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 = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.VERBOSE | re.DOTALL)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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