Android String to URL Convert isUrlValid(String str)

Here you can find the source of isUrlValid(String str)

Description

Check if give string is a valid url

Parameter

Parameter Description
str url string

Return

true:valid url; false:invalid url

Declaration

public static boolean isUrlValid(String str) 

Method Source Code

//package com.java2s;
import java.util.Locale;
import java.util.regex.Pattern;

public class Main {
    private static Locale defloc = Locale.getDefault();
    private static final boolean CASE_NON_SENSITIVE = false;

    /**/*from   w w  w .java2s  .  co  m*/
     * Check if give string is a valid url
     * 
     * @param str   url string
     * @return   true:valid url; false:invalid url
     */
    public static boolean isUrlValid(String str) {
        return isUrlValid(str, CASE_NON_SENSITIVE);
    }

    public static boolean isUrlValid(String str, boolean isCaseSensitve) {
        if (!isCaseSensitve)
            str = str.toLowerCase(defloc);

        String regex = "^((https|http|ftp|rtsp|mms)?://)"
                + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" // ftp user @
                + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP format URL- 199.194.52.184
                + "|" // allow IP and Domain
                + "([0-9a-z_!~*'()-]+\\.)*" // Domain - www.
                + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // 2nd level domain
                + "[a-z]{2,6})" // first level domain- .com or .museum
                + "(:[0-9]{1,4})?" // Port: 80
                + "((/?)|" // a slash isn't required if there is no file name
                + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";

        return Pattern.matches(regex, str);
    }
}

Related

  1. extracURL(String _str)
  2. isUrl(String s)
  3. isUrl(String str)
  4. isUrl(String s)
  5. isHttpUrl(String input)
  6. getDomain(String url)
  7. findUrl(StringBuilder sb)
  8. isImageUrl(String imgUrl)