Skip to content

Instantly share code, notes, and snippets.

@tomcam
Created November 18, 2020 00:30
Show Gist options
  • Save tomcam/996e9e565fc8db4ca41484a369338993 to your computer and use it in GitHub Desktop.
Save tomcam/996e9e565fc8db4ca41484a369338993 to your computer and use it in GitHub Desktop.
Detecting HTML headers with regexp in Go
// Credit to anonymous user at:
// https://play.golang.org/p/OfQ91QadBCH
// Match an h1 in Markdown
h1, _ = regexp.Compile("(?m)^\\s*#{1}\\s*([^#\\n]+)$")
// Match headers 2-6 in Markdown
anyHeader, _ = regexp.Compile("(?m)^\\s*#{2,6}\\s*([^#\\n]+)$")
// Match everything after the pound sign on a line starting with the pound
sign
notPound, _ = regexp.Compile("(?m)[^#|\\s].*$")
// header1() extracts the first h1 it finds in the markdown
func header1(s string) string {
any := h1.FindString(strings.Trim(s, "\n\t\r"))
if any != "" {
return (notPound.FindString(any))
} else {
return ""
}
}
// header2To6() extracts the first h2-h2 it finds.
func header2To6(s string) string {
any := anyHeader.FindString(strings.Trim(s, "\n\t\r"))
if any != "" {
return notPound.FindString(any)
} else {
return ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment