LearnGO/go/pkg/mod/mvdan.cc/xurls/v2@v2.5.0
2024-09-19 21:38:24 -04:00
..
.github whatcanGOwrong 2024-09-19 21:38:24 -04:00
cmd/xurls whatcanGOwrong 2024-09-19 21:38:24 -04:00
generate whatcanGOwrong 2024-09-19 21:38:24 -04:00
.gitattributes whatcanGOwrong 2024-09-19 21:38:24 -04:00
.gitignore whatcanGOwrong 2024-09-19 21:38:24 -04:00
example_test.go whatcanGOwrong 2024-09-19 21:38:24 -04:00
go.mod whatcanGOwrong 2024-09-19 21:38:24 -04:00
go.sum whatcanGOwrong 2024-09-19 21:38:24 -04:00
LICENSE whatcanGOwrong 2024-09-19 21:38:24 -04:00
README.md whatcanGOwrong 2024-09-19 21:38:24 -04:00
schemes.go whatcanGOwrong 2024-09-19 21:38:24 -04:00
tlds_pseudo.go whatcanGOwrong 2024-09-19 21:38:24 -04:00
tlds.go whatcanGOwrong 2024-09-19 21:38:24 -04:00
unicode.go whatcanGOwrong 2024-09-19 21:38:24 -04:00
xurls_test.go whatcanGOwrong 2024-09-19 21:38:24 -04:00
xurls.go whatcanGOwrong 2024-09-19 21:38:24 -04:00

xurls

Go Reference

Extract urls from text using regular expressions. Requires Go 1.19 or later.

import "mvdan.cc/xurls/v2"

func main() {
	rxRelaxed := xurls.Relaxed()
	rxRelaxed.FindString("Do gophers live in golang.org?")  // "golang.org"
	rxRelaxed.FindString("This string does not have a URL") // ""

	rxStrict := xurls.Strict()
	rxStrict.FindAllString("must have scheme: http://foo.com/.", -1) // []string{"http://foo.com/"}
	rxStrict.FindAllString("no scheme, no match: foo.com", -1)       // []string{}
}

Since API is centered around regexp.Regexp, many other methods are available, such as finding the byte indexes for all matches.

The regular expressions are compiled when the API is first called. Any subsequent calls will use the same regular expression pointers.

cmd/xurls

To install the tool globally:

go install mvdan.cc/xurls/v2/cmd/xurls@latest
$ echo "Do gophers live in http://golang.org?" | xurls
http://golang.org