package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)^((http|https|ftp|ftps):\/\/)?(?<url_host>(\[[^\]]+\]|[^\/:\n]+))(?<url_port>(:\d+)?)(\/)?`)
var str = `sony.com
http://cnn.com/story
https://google.com:444/stuff
1.2.3.4:12/ok
1.2.3.4/ok
2.2.2.2:443
/dontmatchme
ftp://mononym/path
http://google.com
www.test
http://[2001:1890:1c00:6500::d:7]/rar/
[2001:1890:1c00:6500::d:7]
[2001:1890:1c00:6500::d:7]/test
http://[2001:1890:1c00:6500::d:7]:8080/rar/
[2001:1890:1c00:6500::d:7]:8080
[2001:1890:1c00:6500::d:7]:4/test
`
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/