Go (RE2)

Markdown Heading in GO

Matches markdown ATX-style headings (# through ######).

Try it in the GO tester →

Pattern

regexGO
^(#{1,6})\s+(.+)$   (flags: gm)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`(?m)^(#{1,6})\s+(.+)$`)
	input := `# Title
## Subtitle
### Section`
	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

`^(#{1,6})` captures 1–6 hash characters at line start. `\s+` requires whitespace. `(.+)$` captures the heading text to end of line.

Examples

Input

# Title ## Subtitle ### Section

Matches

  • # Title
  • ## Subtitle
  • ### Section

Input

####### Not a heading

No match

Same pattern, other engines

← Back to Markdown Heading overview (all engines)