Go (RE2)

C-Style Block Comment in GO

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

Try it in the GO tester →

Pattern

regexGO
\/\*[\s\S]*?\*\/   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`\/\*[\s\S]*?\*\/`)
	input := `/* one */ var x = 1; /* two\nlines */ y = 2;`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

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)