-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.

2
  • 1
    Java is to Javascript as ham is to hamster. Please pay attention to your tags. Commented Jun 24, 2014 at 10:16
  • 1
    URL has a String constructor that throws a MalformedURLException... Commented Jun 24, 2014 at 10:17

1 Answer 1

-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;
}
Sign up to request clarification or add additional context in comments.

9 Comments

This function gives required output but this is correct method to check?
@vijayk Yes what is the problem with that ?
I just confirm . I dont have problem.
Suppose this is the example 11155555.com which is return true but this is not a valid url. Then how can you check it?
|

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.