You need to replace re.MULTILINE
with re.DOTALL
/re.S
and move out period outside the character class as inside it, the dot matches a literal .
.
Note that re.MULTILINE
only redefines the behavior of ^
and $
that are forced to match at the start/end of a line rather than the whole string. The re.DOTALL
flag redefines the behavior of .
inside the pattern outside the character class only. It starts matching a newline symbol, too.
So, the regex you could use for the current example: /*.*?*/
. It matches a literal /*
with /*
, then .*?
matches as few any symbols as possible up to and including */
(matched with */
).
See the code demo:
txt = """
<?php
/* Multi-line
comment */
$var = 1;
"""
new_txt = re.sub(r'/*.*?*/', '', txt, flags=re.S)
print("
=========== TXT ============")
print(txt)
print("
=========== NEW TXT ============")
print(new_txt)
See IDEONE demo
However, it is not the best solution, as in most cases multiline comments are very long. The best is an unrolling-the-loop technique. The regex above can be "unrolled" like this:
/*[^*]*(?:*(?!/)[^*]*)**/
See the regex demo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…