Regexp to match domain and subdomains (in Java)

2019-05-26 07:16发布

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

but next domains are NOT OKAY:

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

标签: java regex url
3条回答
虎瘦雄心在
2楼-- · 2019-05-26 07:31
^https?://[^/@]*\.domain\.com(/.*)?$

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

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.

查看更多
唯我独甜
3楼-- · 2019-05-26 07:35

you can use

"http://a.domain.com".indexOf("domain.com") 

this will return -1 if the requested string is not in the text.

since you can don't want '.' after 'domain.com'

you can can use

"http://a.domain.com".indexOf("domain.com.")

and check whether it is -1 or not

查看更多
地球回转人心会变
4楼-- · 2019-05-26 07:47

Try this:

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

[^.]+\. means one or more non-dot characters, followed by a dot; a quick and dirty way to match a domain-name component and its trailing dot. I wouldn't use that to find domain names in a larger body of text, but it's good enough for the kind of validating you're doing. Put that in a group and add the * quantifier to get a regex that matches zero or more components.

You don't really care what comes after the domain name, but you do have to make sure you've reached the end of it; for example, you don't want to match http://domain.company.com. The final part of the regex, (?![^/]), is a negative lookahead that means if there is another character after this, and that character is not /, fail.

查看更多
登录 后发表回答