【问题标题】:Regex to match domain without subfolder正则表达式匹配没有子文件夹的域
【发布时间】:2016-03-04 16:34:22
【问题描述】:

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

/(([\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

【问题讨论】:

    标签: regex


    【解决方案1】:

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

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

    RegEx Demo

    【讨论】:

      【解决方案2】:

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

      (?: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   
      

      【讨论】: