Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

regex needed to strip out domain name

I need a regexp to strip out just the domain name part of a url. So for example if i had the following url:

http://www.website-2000.com

the bit I'd want the regex to match would be 'website-2000'

If you could also explain which each part of the regex does to help me understand it that would be great.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This one should work. There might be some faults with it, but none that I can think of right now. If anyone want to improve on it, feel free to do so.

/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

http://rubular.com/r/ROz13NSWBQ


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...