Showing posts with label regex url validation. Show all posts
Showing posts with label regex url validation. Show all posts

Wednesday, June 3, 2015

Validate URL Using Java & Regex


class UrlValidator {
    public static void main (String[] args) {
        check("http://google.com");
        check("http://google.com?a=b#99");
        check("http://google.com?a=b#99#sty");
        check("http://www.google.com?a=1&b=2 / 3");
        check("http://www.google.com?a=1&orderRate=orderRateUUID");
    }

    public static void check(String url) {
        String Regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";

        Boolean matches = url.matches(Regex);

        if(matches) {
            System.out.println("URL (YES): " + url);
        }
        else {
            System.out.println("URL (NOT): " + url);
        }
    }
}


URL (YES): http://google.com
URL (YES): http://google.com?a=b#99
URL (YES): http://google.com?a=b#99#sty
URL (NOT): http://www.google.com?a=1&b=2 / 3
URL (YES): http://www.google.com?a=1&orderRate=orderRateUUID