1

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
see
  • 65
  • 1
  • 5

4 Answers4

2
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
  • 12,406
  • 10
  • 42
  • 59
1

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
  • 39,366
  • 20
  • 100
  • 145
  • Might as well do `url.startsWith("example.com/test/test1/example/")` if that's all you're doing. – polygenelubricants Aug 10 '10 at 21:30
  • True... but for performance sake, it is better to precompile the pattern then reuse it again and again later on. – limc Aug 10 '10 at 21:55
  • 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 Aug 11 '10 at 05:02
0

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
  • 11,542
  • 7
  • 41
  • 60
  • i mean the given url should contain certain directories not an valid url sorry for my bad english – see Aug 10 '10 at 21:15
0

This question is answered here using regular expressions: Regular expression to match URLs in 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

Community
  • 1
  • 1
Dubas
  • 2,855
  • 1
  • 25
  • 37