tags:

views:

140

answers:

4

I need to check if an URL is valid or not. The URL should contain some subdirectories like as:

example.com/test/test1/example/a.html

The URL should contain subdirectories test, test1 and example. How can I check if the URL is valid using regex in Java?

A: 

You can simply pass your URL as an argument to the java.net.URL(String) constructor and check if the constructor throws java.net.MalformedURLException.

EDIT If, however, you simply want to check if a given string contains a given substring, use the String.contains(CharSequence) method. For example:

String url = "example.com/test/test1/example/a.html";
if (url.contains("/test/test1/")) {
    // ...
}
Bolo
i mean the given url should contain certain directories not an valid url sorry for my bad english
see
+2  A: 
String url = "example.com/test/test1/example/a.html";
List<String> parts = Arrays.asList(url.split("/"));
return (parts.contains("test") && parts.contains("test1") && parts.contains("example"));
dave
You can get rid of `? true : false`. It's superfluous, the expression already returns a `boolean` anyway :)
BalusC
@BalusC thanks.
dave
What about `example.com/nope.html?query=lol/test/test1/example/lol`?
polygenelubricants
@poly: good one, replace `url.split("/")` by `url.split("\\?", 2)[0].split("/")`.
BalusC
A: 

Since you want to do in regex, how about this...

Pattern p = Pattern.compile("example\\.com/test/test1/example/[\\w\\W]*");

System.out.println("OK: " + p.matcher("example.com/test/test1/example/a.html").find());
System.out.println("KO: " + p.matcher("example.com/test/test2/example/a.html").find());
limc
Might as well do `url.startsWith("example.com/test/test1/example/")` if that's all you're doing.
polygenelubricants
True... but for performance sake, it is better to precompile the pattern then reuse it again and again later on.
limc
No, a compiled regex will still not be faster than String.startsWith(), since startsWith() always just matches the characters one by one, which is what the regular expression needs to do even in the optimal case. Also, your regular expression isn't anchored, and you are using .find(), so it would return true even if the URL was http://my.evil.domain/doBadStuff?url=example.com/test/test2/example/a.html
Avi
A: 

This question is answered here using regular expressions: http://stackoverflow.com/questions/163360/regular-expresion-to-match-urls-java

But you can use the library Apache Commons Validators to use some tested validators instead to write your own.

Here is the library: http://commons.apache.org/validator/

And here the javadoc of the URL Validator. http://commons.apache.org/validator/apidocs/org/apache/commons/validator/UrlValidator.html

Dubas