正则表达式需要删除域名

geo*_*310 1 regex

我需要一个正则表达式来删除网址的域名部分.例如,如果我有以下网址:

http://www.website-2000.com

我希望正则表达式匹配的位是'website-2000'

如果你还可以解释正则表达式的每个部分如何帮助我理解它会很棒.

谢谢

hli*_*set 9

这应该工作.它可能有一些缺点,但我现在无法想到.如果有人想改进它,请随意这样做.

/http:\/\/(?:www\.)?([a-z0-9\-]+)(?:\.[a-z\.]+[\/]?).*/i

http:\/\/            matches the "http://" part
(?:www\.)?           is a non-capturing group that matches zero or one "www."
([a-z0-9\-]+)        is a capturing group that matches character ranges a-z, 0-9
                     in addition to the hyphen. This is what you wanted to extract.
(?:\.[a-z\.]+[\/]?)  is a non-capturing group that matches the TLD part (i.e. ".com",
                     ".co.uk", etc) in addition to zero or one "/"
.*                   matches the rest of the url
Run Code Online (Sandbox Code Playgroud)

http://rubular.com/r/ROz13NSWBQ