如何从 Lua 中的 url 中提取子域名?
例如。如果我有一个网址https://foo.bar.com/path1,我需要像下面这样
get_subdomain("https://foo.bar.com/path1") should return "foo"
如果可能的话,我想要一个可以同时使用http和httpsurl 的解决方案。谢谢
我尝试使用正则表达式,但 Lua 不支持 POSIX 兼容的正则表达式。
你尝试过 string.match 吗?
检查一下:
function get_subdomain(url)
local subdomain = string.match(url, "https?://([^.]+).")
return subdomain
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
239 次 |
| 最近记录: |