Golang - extract links using regex

我需要得到所有的文本链接,在特定的域example.de使用正则表达式在围棋

以下是应提取的所有可能链接:

https://example.de 
https://example.de/
https://example.de/home
https://example.de/home/
https://example.de/home some text that should not be extracted
https://abc.example.de
https://abc.example.de/
https://abc.example.de/home
https://abc.example.de/home
https://abc.example.de/home some text that should not be extracted

我已经试过了

我用这个网站判断我的正则表达式是否正确:https://regex101.com/r/ohxUcG/2

  • https?://*.+example.de*.+在表达式https://abc.example.de/a1b2c3 dsadsa中无法将整个文本发送到\n,而不是没有dsadsahttps://abc.example.de/a1b2c3
  • https?://*.+example.de*.+\s(\w+)$这会得到只以空间终止的链接,但有时链接可以以\n\t等终止.

可能有用的资源

推荐答案

你可以用

(?:https?://)?(?:[^/.]+\.)*\bexample\.de\b(?:/[^/\s]+)*/?

regex demo.Details:

  • (?:https?://)?-可选的http://https://字符串
  • (?:[^/.]+\.)*-一个或多个字符的零或多个序列,而不是/.个字符,然后是.个字符
  • \bexample\.de\b-一个单词example.de
  • (?:/[^/\s]+)*-零次或多次重复/,然后重复除空格和/以外的一个或多个字符
  • /?-可选的/字符.

Go相关问答推荐

Makefile:现有文件上没有这样的文件或目录,不加载环境变量

获取k8s群集作用域运算符的命名空间

验证访问令牌(密钥罩)

如何使用GO GIN从Auth0 JWT内标识检索权限

如何在链接中写入链接

如何模拟go的Elastic search SDK?

Go 是否提供了标准或事实上的方法来处理单个语句中的错误(即内联错误处理)?

使用goroutines在Golang中验证 struct

AWS Lambda 中的 Websocket URL 超时达到错误

判断不同 go map 类型中的重复键

如何使用名称具有包名称的嵌套 struct 启动 go struct

Go 泛型:自引用接口约束

Golang计算 struct struct 中的字段数

如何在Go中替换符号并使下一个字母大写

如何在 Windows 中使用 github.com/AllenDang/giu 和 github.com/gordonklaus/portaudio 构建 GO 程序

gqlgen go,通过添加一个解析器来减少数据库调用

Go generics:我会在哪里使用 any 而不是 interface{}?

如何使用通用字段初始化匿名struct数组

我应该明确地创建一个与Belongs To或Has Many对称的关系吗?

如何动态解析 Go Fiber 中的请求正文?