theundeadmonk
theundeadmonk

Reputation: 265

Extract Subdomain from a url in Lua

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.

Upvotes: 3

Views: 344

Answers (1)

Nicolas Castellanos
Nicolas Castellanos

Reputation: 937

Have you tried string.match?

Check this:

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

Upvotes: 5

Related Questions