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

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

我需要example.de在 Go 中使用 Regex从特定域中的文本获取所有链接

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

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而不是https://abc.example.de/a1b2c3没有dsadsa

  • https?://*.+example.de*.+\s(\w+)$这会获取仅以空格终止的链接,但有时链接可以以\n\t等终止。

慕尼黑8549860
浏览 284回答 1
1回答

森林海

您可以使用(?:https?://)?(?:[^/.]+\.)*\bexample\.de\b(?:/[^/\s]+)*/?请参阅正则表达式演示。详情:(?:https?://)?- 一个可选的http://或https://字符串(?:[^/.]+\.)* - 一个或多个字符的零个或多个序列,除了 a/和.chars,然后是.char\bexample\.de\b- 一个完整的词example.de(?:/[^/\s]+)*- 零次或多次重复/,然后是除空格以外的一个或多个字符,以及//?- 一个可选的/字符。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go