开发者

Validate URL in java

开发者 https://www.devze.com 2023-01-02 09:59 出处:网络
D开发者_C百科oes anyone know how to validate the format of a url in Java using regex? ThanksA very sneaky way is to do:

D开发者_C百科oes anyone know how to validate the format of a url in Java using regex?

Thanks


A very sneaky way is to do:

try {
    new java.net.URI(myUrl);
} catch(URISyntaxException e) {
    // url badly formed
}


You might be better off using the URI class in Java: http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html

It will throw an exception in the constructor if it doesn't parse correctly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号