-1

I have one inputtext Where I have to enter a link .
How I am going to validate that string is a url(link) or not. Example-

Pattern p = Pattern
                .compile("(@)?(href=')?(HREF=')?(HREF=\")?(href=\")?(http://)?[a-zA-Z_0-9\\-]+(\\.\\w[a-zA-Z_0-9\\-]+)+(/[#&\\n\\-=?\\+\\%/\\.\\w]+)?");

        Matcher m = p.matcher("https://mail.google.com");
        System.out.println(m.matches());

This code gives also false.I am not getting what was the problem in that code.
Or please refer another logic to validate the url.
I had already used UrlValidator urlValidator = new UrlValidator();
But I dont want to use this .jar

This is the another method this method validate properly but I that url is not available.How to check this requirement? example-

public static void main(String[] args) {
        String b = "http://11155555.com/";
        System.out.println(isUrl(b));

    }

    public static boolean isUrl(String str) {
        Pattern urlPattern = Pattern.compile(
                "((https?|ftp|gopher|telnet|file):((//)|(\\\\\\\\))+[\\\\w\\\\d:#@%/;$()~_?\\\\+-=\\\\\\\\\\\\.&]*)",
                Pattern.CASE_INSENSITIVE);
        Matcher matcher = urlPattern.matcher(str);
        if (matcher.find()) {
            return true;
        } else {
            return false;
        }
    }

Thanks in advanced.

vijayk
  • 2,633
  • 14
  • 38
  • 59

1 Answers1

-1

Try this one :

 public static boolean isUrl(String str){
    Pattern urlPattern = Pattern.compile("((https?|ftp|gopher|telnet|file):((//)|(\\\\\\\\))+[\\\\w\\\\d:#@%/;$()~_?\\\\+-=\\\\\\\\\\\\.&]*)",Pattern.CASE_INSENSITIVE);
         Matcher matcher = urlPattern.matcher(str);
    if (matcher.find()) 
             return true;
        else
            return false;
}
Keval Trivedi
  • 1,290
  • 2
  • 13
  • 29