Go (RE2)

Markdown Image in GO

Matches Markdown image syntax ![alt](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 := `![logo](https://example.com/logo.png)`
	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

`!\[([^\]]*)\]` captures the alt text inside brackets. `\(([^)]+)\)` captures the URL in parentheses.

Examples

Input

![logo](https://example.com/logo.png)

Matches

  • ![logo](https://example.com/logo.png)

Input

![](image.jpg) and ![alt](pic.gif)

Matches

  • ![](image.jpg)
  • ![alt](pic.gif)

Same pattern, other engines

← Back to Markdown Image overview (all engines)