package main import ( "log" "regexp" ) var ( alias = regexp.MustCompile(`\|(.*)`) link = regexp.MustCompile(`^([a-zA-Z ]+)?[\|\#\^\\]?`) subsection = regexp.MustCompile(`#([\w\s]+)`) block = regexp.MustCompile(`#\^([a-zA-Z ]+)`) wholeLink = regexp.MustCompile(`\[\[(.*)\]\]`) ) type Link struct { Dest, Alias, Subsection, Block string } func ExtractLink(raw string) Link { var li Link rawLinkMatch := wholeLink.FindAllStringSubmatch(raw, -1) var rawLink string if len(rawLinkMatch) > 0 { rawLink = rawLinkMatch[0][1] } l := link.FindAllStringSubmatch(rawLink, -1) if len(l) > 0 { li.Dest = l[0][1] } a := alias.FindAllStringSubmatch(rawLink, -1) if len(a) > 0 { li.Alias = a[0][1] } s := subsection.FindAllStringSubmatch(rawLink, -1) if len(s) > 0 { li.Subsection = s[0][1] } b := block.FindAllStringSubmatch(rawLink, -1) if len(b) > 0 { li.Block = b[0][1] } return li } func main() { log.Printf("%#v\n", ExtractLink(`Embedding a link in a bigger block of text [[Regular Link#^link to block]] shouldn't cause any problems `)) }