Golang - 使用正则表达式提取链接

Golang - extract links using regex

Golang - 使用正则表达式提取链接

我需要在 Go 中使用正则表达式从特定域 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?://)?(?:[^/.]+\.)*\bexample\.de\b(?:/[^/\s]+)*/?

regex demo详情:

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