Markdown Link in GO
Match Markdown links [link text](url) and capture both the display text and the URL.
Try it in the GO tester →Pattern
regexGO
\[([^\]]+)\]\(([^)]+)\) (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\[([^\]]+)\]\(([^)]+)\)`)
input := `[RegexPro](https://www.regexpro.dev)`
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
Group 1 captures the link text inside square brackets (any chars except ]). Group 2 captures the URL inside parentheses (any chars except )).
Examples
Input
[RegexPro](https://www.regexpro.dev)Matches
[RegexPro](https://www.regexpro.dev)
Input
[Click here](https://example.com/path)Matches
[Click here](https://example.com/path)