目前,我使用以下正则表达式匹配网址
/(([\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
答案 0 :(得分:1)
答案 1 :(得分:0)
以下正则表达式也值得一试:
(?:https?://)?([^/\s]+\.[^/\s]+)/?(?:\s|$)
<强>解释强>
(?: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