package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)(?<!\\) # Check it was not escaped with a \
(?<img_all>
\!\[(?<img_alt>.+?)\] # image alternative text, i.e. the text used when the image does not
(?:
(?:
[ ]? # possibly followed by some spaces
(?:\n[ ]*)? # and a new line with some space
\[(?<img_id>.*?)\] # with the link id in brackets, but may be empty
)
|
(?:
(?:
\(
[ \t]*
<(?<img_url> # link url within <>; or
.+?
)>
[ \t]* # possibly followed by some spaces or tabs
(?:
(?<img_title_container>['"]) # Title is surrounded ether by double or single quotes
(?<img_title>.*?) # actual title, but could be empty as in ""
\g{img_title_container} # make the sure enclosing mark balance
)?
[ \t]*
\)
)
|
(?:
\(
[ \t]*
(?<img_url> # link url within <>; or
(?:((?!["']).)*+|.*)
)
[ \t]*
(?:
(?<img_title_container>['"]) # Title is surrounded ether by double or single quotes
(?<img_title>.*?) # actual title, but could be empty as in ""
\g{img_title_container} # make the sure enclosing mark balance
)?
[ \t]*
\)
)
)
)
[ \t]*
(?<!\\)
\{
[[:blank:]\h]*
(?<img_attr>.+?)
[[:blank:]\h]*
\}
)`)
var str = `This is an {.class #inline-img}.
.jpg'Title here') {.class #inline-img} and some text after
 {.class #inline-img}
`
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", i)
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Golang, please visit: https://golang.org/pkg/regexp/