How do I extract a subdomain from a url in Lua?
eg. if I have a url https://foo.bar.com/path1, I need to to something like the following
get_subdomain("https://foo.bar.com/path1") should return "foo"
If possible, I'd like a solution that works with both http and https urls.
Thanks
I tried using a regex, but Lua does not support POSIX compliant Regexes.
Have you tried string.match?
Check this:
function get_subdomain(url)
local subdomain = string.match(url, "https?://([^.]+).")
return subdomain
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With