Q:

Derive host from the URL in LUA

I have following URL.
http://localhost:4000/path?query=foo
http://localhost:4000/
http://localhost
http://localhost/

All I want is to return the host part. To be honest I do not care about the URL like this
 http://localhost:abcd/
 http://localhost:abcd/path?query=foo

because it's is guaranteed to correct URL.
I somehow manage to derive some pattern for it on rubular
But this involves lookahead technique how do I apply lookahead technique. 
Which look like this 
^https?:\/\/(.+)(?=[\/|$])

but there are 2 problems 

Lookahead technique does not work with lua match
The regex is not full proof at least for the following http://localhost (notice the slash missing at the end)

So this is my question.
How do I solve this?  

A:

The key to pattern matching is avoiding special cases, such as optional delimiters. Appending / to the string simplifies the task.
Try this code:
function host(s)
    return (s.."/"):match("://(.-)/")
end

function test(s)
    print(s,host(s))
end

test"http://localhost:4000/path?query=foo"
test"http://localhost:4000/"
test"http://localhost"
test"http://localhost/"

