正则表达式匹配域没有子文件夹

时间:2016-03-04 16:34:23

标签: regex

目前,我使用以下正则表达式匹配网址

/(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,63}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?/

我想知道如何将其修改为仅匹配没有子文件夹的域名。

例如

http://thisiatest.com/ -> Good
thisisatest.com -> Good
http://thisiatest.com -> Good
http://thisisatest.com/folder/ -> Bad
thisisatest.com/folder/ -> Bad

2 个答案:

答案 0 :(得分:1)

我认为你的正则表达式可以简化为:

^(?:\S+://)?[^/]+/?$

RegEx Demo

答案 1 :(得分:0)

以下正则表达式也值得一试:

(?:https?://)?([^/\s]+\.[^/\s]+)/?(?:\s|$)

Here is the Demo.

<强>解释

(?:https?://)?          non-capturing group starts
                        match http:// or https:// zero or one time
(                       capturing group starts
[^/\s]+                 match characters except / and space
                        1 or more times
\.                      literally match dot (.)
[^/\s]+                 match characters except / and space
                        1 or more times 
)                       capturing group ends
/?                      match / zero or one time    
(?:\s|$)                non-capturing group
                        assert any white space or end of line