Skip to content

mvdan/xurls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Feb 22, 2025
1094ff3 · Feb 22, 2025
Feb 11, 2025
Feb 22, 2025
Feb 22, 2025
Dec 24, 2021
May 25, 2015
Feb 7, 2016
Feb 11, 2025
Jan 1, 2024
Feb 11, 2025
Jan 2, 2025
Feb 22, 2025
Jan 2, 2025
Aug 3, 2022
May 19, 2023
Jan 2, 2025
Feb 22, 2025

Repository files navigation

xurls

Go Reference

Extract urls from text using regular expressions. Requires Go 1.23 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