Home > OS >  Extract Subdomain from a url in Lua
Extract Subdomain from a url in Lua

Time:12-14

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.

CodePudding user response:

Have you tried string.match?

Check this:

function get_subdomain(url)
  local subdomain = string.match(url, "https?://([^.] ).")
  return subdomain
end
  • Related