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?://*.+example.de*.+
在表达式 https://abc.example.de/a1b2c3 dsadsa
上失败将整个文本获取到 \n
而不是 https://abc.example.de/a1b2c3
没有 dsadsa
https?://*.+example.de*.+\s(\w+)$
这会获取仅以 space 终止的链接,但有时链接可以以 \n
或 \t
等终止。
可能有用的资源
你可以使用
(?:https?://)?(?:[^/.]+\.)*\bexample\.de\b(?:/[^/\s]+)*/?
见regex demo。 详情:
(?:https?://)?
- 一个可选的 http://
或 https://
字符串
(?:[^/.]+\.)*
- 除了 /
和 .
字符之外的一个或多个字符的零个或多个序列,然后是 .
字符
\bexample\.de\b
- 一个完整的单词 example.de
(?:/[^/\s]+)*
- /
的零次或多次重复,然后是空格和 /
以外的一个或多个字符
/?
- 一个可选的 /
字符。
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?://*.+example.de*.+
在表达式https://abc.example.de/a1b2c3 dsadsa
上失败将整个文本获取到\n
而不是https://abc.example.de/a1b2c3
没有dsadsa
https?://*.+example.de*.+\s(\w+)$
这会获取仅以 space 终止的链接,但有时链接可以以\n
或\t
等终止。
可能有用的资源
你可以使用
(?:https?://)?(?:[^/.]+\.)*\bexample\.de\b(?:/[^/\s]+)*/?
见regex demo。 详情:
(?:https?://)?
- 一个可选的http://
或https://
字符串(?:[^/.]+\.)*
- 除了/
和.
字符之外的一个或多个字符的零个或多个序列,然后是.
字符\bexample\.de\b
- 一个完整的单词example.de
(?:/[^/\s]+)*
-/
的零次或多次重复,然后是空格和/
以外的一个或多个字符
/?
- 一个可选的/
字符。