Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Subdomain from a url in Lua

Tags:

nginx

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.

like image 235
theundeadmonk Avatar asked Nov 19 '25 17:11

theundeadmonk


1 Answers

Have you tried string.match?

Check this:

function get_subdomain(url)
  local subdomain = string.match(url, "https?://([^.]+).")
  return subdomain
end
like image 117
Nicolas Castellanos Avatar answered Nov 21 '25 08:11

Nicolas Castellanos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!