2

Hey I'm trying a url validation based upon What is the best regular expression to check if a string is a valid URL? in java but for some reason it's not working. Suggestions?

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class urlValidate {

    /**
     * @param args
     */
    public static void main(String[] args) {
        test_url("http://brb/", false);
            test_url("https://localserver/projects/public/assets/javascript/widgets/UserBoxMenu/widget.css", false);
    test_url("https://www.google.com/", true);
    test_url("https://www.google.co.uk/projects/my%20folder/test.php", false);
    test_url("https://myserver.localdomain/", true);
    test_url("https://192.168.1.120/projects/index.php/", false);
    test_url("https://192.168.1.1/", true);
    test_url("https://projectpier-server.localdomain/projects/public/assets/javascript/widgets/UserBoxMenu/widget.css", false);
    test_url("https://2.4.168.19/project-pier?c=test&a=b", false);
    test_url("https://localhost/a/b/c/test.php?c=controller&arg1=20&arg2=20", false);
    test_url("https://user:password@localhost/a/b/c/test.php?c=controller&arg1=20&arg2=20", false);
    test_url("myserver",false);
    test_url("https://tomcat:8080/",true);
    test_url("https://facebook.com",false);
}

public static void test_url(String url, boolean expected) {
    boolean valid = isURLValid(url, true);
    String out = "URL Valid?: " + (valid ? "yes" : "no") + " for URL: "
            + url + ". Expected: " + (expected ? "yes" : "no") + ". ";
    if (valid == expected) {
        out += "PASS\n";
    } else {
        out += "FAIL\n";
    }
    System.out.println(out);
}

public static boolean isURLValid(String url, boolean forcehttps) {
    String regex = "";
    if (forcehttps) {
        regex = "/^(https):\\/\\/";
    } else {
        regex = "/^(https?):\\/\\/";
    }
    regex += "((([a-z0-9]\\.|[a-z0-9][a-z0-9-]*[a-z0-9]\\.)*"
            + "[a-z][a-z0-9-]*[a-z0-9]"
            + "|((\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])\\.){3}"
            + "(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])"
            + ")(:\\d+)?)"
            + "(#([a-z0-9$_\\.\\+!\\*\\'\\(\\),;:@&=-]|%[0-9a-f]{2})*)?(\\/)"
            + "$/i";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(url); // get a matcher object
    return m.matches();
}

}
3
  • 1
    @SotiriosDelimanolis -Actually, it doesn't. It only cares if a protocol exists. Commented Feb 21, 2013 at 18:27
  • @BrianRoach Good stuff, thanks. Commented Feb 21, 2013 at 18:29
  • 1
    Personally, I wouldn't do complex URL validation for the same reason I wouldn't do complex email address validation. Take a look at davidcel.is/blog/2012/09/06/…. Commented Feb 21, 2013 at 18:31

3 Answers 3

2

The regex is initially wrapped in slashes (to serve as delimiters, which are required for PCRE in PHP). Java does not use these.

if (forcehttps) {
    regex = "^(https):\\/\\";
} else {
    regex = "^(https?):\\/\\";
}

The /i at the end is also undesirable. Instead, write

Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the Apache commons-validator api. There is a class named UrlValidator, or something like that.
Take a look at this: http://commons.apache.org/validator/
I dont't understand a lot of regex, so I can't help you much in this subject.
Good Luck.

1 Comment

While that does accomplish the task at hand, unless you need something else from apache commons that's a pretty large dependency to attach to your project just to validate an URL.
0

Although it is not a problem to write regex to URL validation why not just to use java.io.URL class? Just create instance of URL like following: new URL(spec) and it will throw MalformedURLExcption if syntax is wrong.

1 Comment

according to the javadoc it will only throw that if the protocol is missing or the spec is null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.