Python (re)

HTML Comment in PY

Match HTML comments, including multi-line comments and empty ones.

Try it in the PY tester →

Pattern

regexPY
<!--[\s\S]*?-->   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"<!--[\s\S]*?-->")
input_text = "<!-- hello -->"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

Opens with <!--, then lazily matches any characters (including newlines via [\s\S]) until the first -->. Lazy quantifier prevents greedy spanning across multiple comments.

Examples

Input

<!-- hello -->

Matches

  • <!-- hello -->

Input

<p>keep</p><!-- remove --><span>keep</span>

Matches

  • <!-- remove -->

Input

no comments here

No match

Same pattern, other engines

← Back to HTML Comment overview (all engines)