GigaPr
GigaPr

Reputation: 5386

Validate URL in java

Does anyone know how to validate the format of a url in Java using regex?

Thanks

Upvotes: 3

Views: 6130

Answers (2)

Finbarr
Finbarr

Reputation: 32186

A very sneaky way is to do:

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

Upvotes: 7

dplass
dplass

Reputation: 1473

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.

Upvotes: 7

Related Questions