使用正则表达式从URL提取顶级域和二级域 [英] Extracting top-level and second-level domain from a URL using regex
本文介绍了使用正则表达式从URL提取顶级域和二级域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用正则表达式从URL中仅提取顶级域名和二级域名?我想跳过所有较低级别的域。有想法吗?
How can I extract only top-level and second-level domain from a URL using regex? I want to skip all lower level domains. Any ideas?
推荐答案
这是我的想法,
使用 $
锚点从行尾匹配3个非点。
Match anything that isn't a dot, three times, from the end of the line using the $
anchor.
字符串末尾的最后一个匹配应该是可选的,以允许 .com.au
或 .co.nz
域类型。
The last match from the end of the string should be optional to allow for .com.au
or .co.nz
type of domains.
倒数第二个匹配项仅匹配2-3个字符,因此不会将其与第二个字符混淆级别的域名。
Both the last and second last matches will only match 2-3 characters, so that it doesn't confuse it with a second-level domain name.
正则表达式:
[^。] * \。[^。] {2,3}(?: \。[^。] {2,3})?$
演示:
这篇关于使用正则表达式从URL提取顶级域和二级域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文