is string a Domain Name by regex - Java java.util.regex

Java examples for java.util.regex:Match URL

Description

is string a Domain Name by regex

Demo Code

import java.util.regex.Pattern;

public class Main {
  public static void main(String[] argv) {
    String input = "java2s.com";
    System.out.println(isDomainName(input));
  }/*from  w w w  .jav a 2s . c  o m*/

  public static boolean isDomainName(String input) {
    if (isEmpty(input))
      return false;
    Pattern pattern = Pattern.compile("[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+");
    return pattern.matcher(input).matches();
  }

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

  public static String trim(String str) {
    return (str == null) ? null : str.trim();
  }
}

Related Tutorials