Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang regex to find urls in a string

Tags:

regex

go

I am tring to find all links in a string and then hyperlink them
like this js lib https://github.com/bryanwoods/autolink-js

i tried to use alot of regex but i always got too many errors
http://play.golang.org/p/iQiccXvFiB
i don't know if go has a different regex syntax

so, what regex that works in go that is good to match urls in strings

thanks

like image 304
Marwan Avatar asked Jan 09 '23 13:01

Marwan


1 Answers

You can use xurls:

import "mvdan.cc/xurls"

func main() {
        xurls.Relaxed().FindString("Do gophers live in golang.org?")
        // "golang.org"
        xurls.Relaxed().FindAllString("foo.com is http://foo.com/.", -1)
        // ["foo.com", "http://foo.com/"]
        xurls.Strict().FindAllString("foo.com is http://foo.com/.", -1)
        // ["http://foo.com/"]
}
like image 159
Daniel Martí Avatar answered Jan 19 '23 08:01

Daniel Martí