Does anyone know how to validate the format of a url in Java using regex?
Thanks
Does 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
}
URI class, thanks for pointing this out.new java.net.URI(myUrl), then better avoid the invocation whatsoever. Checking the validity of a URI calls for a URIValidator class which should - to the best of my knowledge - make use of regex (which I'm still looking for around the net. I'll let you know as soon as I find something).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.