【发布时间】:2012-10-07 20:05:01
【问题描述】:
我需要能够识别任何子域的域名。
例子:
对于所有这些,我只需要匹配 example.co / example.com / example.org / example.co.uk / example.com.au / example.gov.us 等等
www.example.co
www.first.example.co
first.example.co
second.first.example.co
no.matter.how.many.example.co
first.example.co.uk
second.first.example.co.uk
no.matter.how.many.example.co.uk
first.example.org
second.first.example.org
no.matter.how.many.example.org
first.example.gov.uk
second.first.example.gov.uk
no.matter.how.many.example.gov.uk
我整天都在玩正则表达式,整天都在谷歌上搜索,但似乎还是找不到。
Edit2:我更喜欢在 t.co 等非常奇怪的情况下可能会失败的正则表达式,然后列出所有 TLD 并列出我没有列出但可能被预测失败并匹配更多的 TLD。这不是您会选择的选项吗?
更新: 使用选择的答案作为指导,我构建了这个为我完成工作的正则表达式。
/([0-9a-z-]{2,}\.[0-9a-z-]{2,3}\.[0-9a-z-]{2,3}|[0-9a-z-]{2,}\.[0-9a-z-]{2,3})$/i
它可能并不完美,但到目前为止我还没有遇到过失败的情况。
【问题讨论】:
标签: regex