How to extract a domain from a url

I need to extract the domain ( four.five ) from the URL ( one.two.three.four.five ) in a Lua string variable.

I cannot find a function to do this in Lua.

EDIT:

By the time the URL reaches me, the http material has already been disabled. So, some examples:

 a) safebrowsing.google.com b) i2.cdn.turner.com c) powerdns.13854.n7.nabble.com 

so my result should be:

 a) google.com b) turner.com c) nabble.com 
+4
source share
3 answers

This should work:

 local url = "foo.bar.google.com" local domain = url:match("[%w%.]*%.(%w+%.%w+)") print(domain) 

Conclusion: google.com

The pattern [%w%.]*%.(%w+%.%w+) searches for content after the second dot . from the end.

+3
source
 local url = "http://foo.bar.com/?query" print(url:match('^%w+://([^/]+)')) -- foo.bar.com 

This pattern '^%w+://([^/]+)' means: ^ from the beginning of the line, take% w + one or more alphanumeric characters (this is the protocol), then: //, then [^ /] + 1 or more characters except slash and return (capture) of these characters as a result.

+3
source

Use Paul's answer to retrieve a domain, e.g. 1.2.3.4.4.5

local url = " http://foo.bar.com/?query " local domain = url: match ('^% w +: // ([^ /] +)'))

and the subsequent use of split methods to assemble an array for parts

http://lua-users.org/wiki/SplitJoin

as

local arr = split (domain, '%.') - the programmed point, because it is part of the "patterns"

Then you can use the last two: arr [#arr], arr [# arr-1]

0
source

Source: https://habr.com/ru/post/1501947/


All Articles