Utility classes for common java regular expressions


This article example for everyone to share java regular expression tool class specific code, for your reference, the specific content is as follows

import com.google.common.base.Strings;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *  Commonly used regular expressions
 * Created by tookbra on 2016/4/7.
 */
public class RegexUtils {
  /**
   *  Determine if it's true IP address
   *
   * @param ip
   * @return boolean true, Through the, false , didn't pass
   */
  public static boolean isIp(String ip) {
    if (Strings.isNullOrEmpty(ip))
      return false;
    String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
        + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
        + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
        + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
    return ip.matches(regex);
  }
  /**
   *  Determine if it is the correct email address
   *
   * @param email
   * @return boolean true, Through the, false , didn't pass
   */
  public static boolean isEmail(String email) {
    if (Strings.isNullOrEmpty(email))
      return false;
    String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    return email.matches(regex);
  }
  /**
   *  To determine whether it contains Chinese characters, only Chinese characters, not punctuation
   * @param text
   * @return boolean true, Through the, false , didn't pass
   */
  public static boolean isChinese(String text) {
    if (Strings.isNullOrEmpty(text))
      return false;
    Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    Matcher m = p.matcher(text);
    return m.find();
  }
  /**
   *  Determine if it is a positive integer
   *
   * @param number
   *       digital
   * @return boolean true, Through the, false , didn't pass
   */
  public static boolean isNumber(String number) {
    if (Strings.isNullOrEmpty(number))
      return false;
    String regex = "[0-9]*";
    return number.matches(regex);
  }
  /**
   *  Determine the decimal places ( A positive number )
   *
   * @param decimal
   *       digital
   * @param count
   *       Decimal digits
   * @return boolean true, Through the, false , didn't pass
   */
  public static boolean isDecimal(String decimal, int count) {
    if (Strings.isNullOrEmpty(decimal))
      return false;
    String regex = "^(-)?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){" + count
        + "})?$";
    return decimal.matches(regex);
  }
  /**
   *  Determine whether it is a mobile phone number
   *
   * @param phoneNumber
   *       Mobile phone number
   * @return boolean true, Through the, false , didn't pass
   */
  public static boolean isMobilePhoneNumber(String phoneNumber) {
    if (Strings.isNullOrEmpty(phoneNumber))
      return false;
    String regex = "^((13[0-9])|(15[0-9])|(18[1-9]))\\d{8}$";
    return phoneNumber.matches(regex);
  }

  /**
   *  Determine if it's a cell phone number
   *
   * @param phoneNumber
   *       Mobile phone number
   * @return boolean true, Through the, false , didn't pass
   */
  public static boolean isPhoneNumber(String phoneNumber) {
    if (Strings.isNullOrEmpty(phoneNumber))
      return false;
    String regex = "^1\\d{10}$";
    return phoneNumber.matches(regex);
  }
  /**
   *  Determine whether it contains special characters
   *
   * @param text
   * @return boolean true, Through the, false , didn't pass
   */
  public static boolean hasSpecialChar(String text) {
    if (Strings.isNullOrEmpty(text))
      return false;
    if (text.replaceAll("[a-z]*[A-Z]*\\d*-*_*\\s*", "").length() == 0) {
      //  If no special characters are included
      return true;
    }
    return false;
  }

  private static boolean isChinese(char c) {
    Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
    if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
        || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
        || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
        || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
        || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
        || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
        || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
      return true;
    }
    return false;
  }
}