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
### SectionMatches
# Title## Subtitle### Section
Input
####### Not a headingNo match
—