php regex 域字符串是否有 www 和顶级域
当谈到正则表达式时,我有点业余,但考虑到我有以下三个域
www.google.com
www.google.co.uk
google.com
,我想创建一些正则表达式来测试该域是否有 www
和 .co.uk
或 .com
有人知道一些可以对此进行测试的正则表达式吗?
I'm abit of an amateur when it comes to regex but consider i have the following three domains
www.google.com
www.google.co.uk
google.com
I would like to create some regex that will test to see if the domain has a www
and also a .co.uk
or .com
Does anybody know of some regex that will test for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不需要正则表达式,你可以使用 strpos 据说比正则表达式更快。
如果您确实想要慢一些并使用正则表达式,您可以像这样测试所有这些
如果例如您想对 www 应用与 .com 不同的逻辑,您可以使用
You don't need regex for this , you can use strpos witch is sayd to be faster than regex .
If you realy whant to be slower and use regex you can test for all of them like this
If for example you whant to apply different logic for www than for .com you can use
试试这个:
Try this:
根据我对您问题的理解,域名需要以
www
开头,并以.co.uk
或.com
结尾。所以这是正则表达式:From what I understand from your question, the domain needs to start with
www
AND ends with either.co.uk
OR.com
. So here is the RegExp:简单
条件
Simple
Conditional