Python (re)

C-Style Block Comment in PY

Match C-style /* ... */ block comments across multiple lines.

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 = "/* one */ var x = 1; /* two\nlines */ y = 2;"
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

\/\* matches the literal `/*`. [\s\S]*? lazily matches any characters including newlines (the [\s\S] idiom avoids needing a dotAll/s flag). \*\/ matches the closing `*/`. Lazy matching ensures adjacent comment blocks aren't merged into one giant match.

Examples

Input

/* one */ var x = 1; /* two\nlines */ y = 2;

Matches

  • /* one */
  • /* two\nlines */

Input

/** JSDoc here */

Matches

  • /** JSDoc here */

Input

// not a block comment

No match

Same pattern, other engines

← Back to C-Style Block Comment overview (all engines)