Android String to URL Convert isUrlValid(String str, boolean isCaseSensitve)

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

Description

is Url Valid

Declaration

public static boolean isUrlValid(String str, boolean isCaseSensitve) 

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. j  a v  a2  s  .  c om
     * 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. preUrl(String url)
  2. TruncateUrlPage(String strURL)
  3. isValidUrl(String url)
  4. imageUrl_2_ImageName(String imageUrl)
  5. extractUrls(String text)