is string a Url starting with http:// or https:// - Java java.util.regex

Java examples for java.util.regex:Match URL

Description

is string a Url starting with http:// or https://

Demo Code

import java.util.regex.Pattern;

public class Main {

  public static void main(String[] argv) {
    String url = "java2s.com";
    System.out.println(isUrl(url));

    url = "http://java2s.com";
    System.out.println(isUrl(url));
  }/* www  .j  a  v  a 2s .c  o m*/

  private final static Pattern URL = Pattern
      .compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~/])+$");

  public static boolean isUrl(String url) {
    return !isEmpty(url) && URL.matcher(url).matches();
  }

  public static boolean isEmpty(String str) {
    return str == null || str.trim().length() == 0 || str.trim().equalsIgnoreCase("null");
  }

}

Related Tutorials