Lua中从url中提取子域名

the*_*onk 2 lua nginx

如何从 Lua 中的 url 中提取子域名?

例如。如果我有一个网址https://foo.bar.com/path1,我需要像下面这样

get_subdomain("https://foo.bar.com/path1") should return "foo"

如果可能的话,我想要一个可以同时使用httphttpsurl 的解决方案。谢谢

我尝试使用正则表达式,但 Lua 不支持 POSIX 兼容的正则表达式。

Nic*_*nos 5

你尝试过 string.match 吗?

检查一下:

function get_subdomain(url)
  local subdomain = string.match(url, "https?://([^.]+).")
  return subdomain
end
Run Code Online (Sandbox Code Playgroud)