douduan9391
2014-10-25 09:47
浏览 988
已采纳

Golang正则表达式以查找字符串中的网址

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

图片转代码服务由CSDN问答提供 功能建议

我正试图在字符串中找到所有链接,然后将它们链接为超链接
就像这个js lib https://github.com/bryanwoods/autolink-js

我尝试使用很多正则表达式,但我总是遇到太多错误
http:// play .golang.org / p / iQiccXvFiB
i不知道go是否具有不同的regex语法

所以,在go中有效的正则表达式是什么 匹配字符串中的网址

谢谢

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doujue6196 2014-10-25 10:52
    已采纳

    Use back-ticks instead of double-quotes for your string literals. Back-slashes inside double-quotes start escape sequences, which you don't need/want for this use case.

    Additionally, how did you expect this to work?

    "<a href="$0">$0</a>"
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongmei425373 2015-05-23 09:19

    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/"]
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题