去除域名所需的正则表达式 [英] regex needed to strip out domain name

查看:44
本文介绍了去除域名所需的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

谢谢

推荐答案

这个应该有效.它可能有一些错误,但我现在想不出.如果有人想改进它,请随时这样做.

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

这篇关于去除域名所需的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆