Regex matches hostname - not including TLD

I need to match the host name - but I do not want tld:

example.com = ~ / regex / => example

sub.example.com = ~ / regex / => sub.example

sub.sub.example.com = ~ / regex / => sub.example

Any help with regex? Thanks.

+3
source share
6 answers

Assuming your string is properly formatted and doesn't include things like a protocol [t. http: //] , you need all the characters, but not including the final .tld.

So this is the easiest way to do this. The regex trick is not to re-complement things:

.*(?=\.\w+)

, , [] .xxx, .

lookahead, , , :

(\w+\.)+

, '.' ".".

+4

/.+(?=\.\w+$)/

? =

/(.+)\.\w+$/

+1

tld:

s/\.[^\.]*$//;
-1
(.*)\.

tlds, . TLD - , -.

-2
(?<Domain>.*)\.(?<TLD>.*?)$
-2

I don’t understand how you want the work to fit. but with a regular extended regular expression you should be able to match any tld with [a-zA-Z]{2,3}So, if you are trying to get all the name other than tld, something like

\(.\)\.[a-zA-Z]{2,3}$

should be close.

-3
source

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


All Articles