正则表达式匹配域和子域(在 Java 中) [英] Regexp to match domain and subdomains (in Java)

查看:35
本文介绍了正则表达式匹配域和子域(在 Java 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证给定的 URL 是否与我的域掩码匹配.

I need to verify if given URL matches my domain mask.

示例:我只想允许满足此伪掩码"的域:

Example: I want to allow only domains which satisfy this "pseudo-mask":

https://*.domain.com
http://*.domain.com

所以下一个域名没问题:

So next domains are OKAY:

http://my.domain.com/something/blah.html
https://www.domain.com/
http://domain.com/go/somewhere.html
https://very.weird.domain.com/index.jsp

但下一个域名不行:

https://domain.com.google.com/other.html
http://my.domainfake.com/haha.jsp
https://my.fakedomain.com/

推荐答案

^https?://[^/@]*\.domain\.com(/.*)?$

(not-/ 用于阻止 .domain.com 出现在路径中,not-@ 用于阻止 username:password@滥用.)

(The not-/ to stop .domain.com appearing in the path, the not-@ to stop username:password@ abuse.)

更好:使用 URL 内置于 Java 中的类以正确解析 URL.然后您可以阅读 host 属性并检查它是否endsWith 您的域.

Better, though: use the URL class built into Java to parse a URL properly. You can then just read the host property and check that it endsWith your domain.

这篇关于正则表达式匹配域和子域(在 Java 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆