Java工具类(26)—最全正则工具类(提供验证邮箱、手机号、电话号码、身份证号码、数字等方法)
提供验证邮箱、手机号、电话号码、身份证号码、数字等方法
package com.awifi.cloudnative.container.openstack.utils;
import java.util.regex.Pattern;
public final class RegexUtils {
public static boolean checkEmail(String email) {
String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
return Pattern.matches(regex, email);
}
public static boolean checkIdCard(String idCard) {
String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";
return Pattern.matches(regex, idCard);
}
public static boolean checkMobile(String mobile) {
String regex = "(\\+\\d+)?1[3458]\\d{9}$";
return Pattern.matches(regex, mobile);
}
public static boolean checkPhone(String phone) {
String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";
return Pattern.matches(regex, phone);
}
public static boolean checkDigit(String digit) {
String regex = "\\-?[1-9]\\d+";
return Pattern.matches(regex, digit);
}
public static boolean checkDecimals(String decimals) {
String regex = "\\-?[1-9]\\d+(\\.\\d+)?";
return Pattern.matches(regex, decimals);
}
public static boolean checkBlankSpace(String blankSpace) {
String regex = "\\s+";
return Pattern.matches(regex, blankSpace);
}
public static boolean checkChinese(String chinese) {
String regex = "^[\u4E00-\u9FA5]+$";
return Pattern.matches(regex, chinese);
}
public static boolean checkBirthday(String birthday) {
String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
return Pattern.matches(regex, birthday);
}
public static boolean checkURL(String url) {
String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?";
return Pattern.matches(regex, url);
}
public static boolean checkPostcode(String postcode) {
String regex = "[1-9]\\d{5}";
return Pattern.matches(regex, postcode);
}
public static boolean checkIpAddress(String ipAddress) {
String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))";
return Pattern.matches(regex, ipAddress);
}
public static boolean checkOsName(String osName, Integer n) {
String regex = "^[a-z0-9]{0,1}[a-z0-9_]{0," + (n - 3) + "}[a-z0-9]{0,1}$";
return Pattern.matches(regex, osName);
}
public static boolean checkPassword(String password, Integer n, Integer m) {
String regex = "^[a-zA-Z0-9_]{" + (n - 1) + "," + (m - 1) + "}$";
return Pattern.matches(regex, password);
}
public static boolean checkLetter(String letter, Integer n) {
String regex = "^.{0," + n + "}$";
return Pattern.matches(regex, letter);
}
public static boolean checkNum(String num, Integer n) {
String regex = "^\\d{0," + n + "}$";
return Pattern.matches(regex, num);
}
public static boolean checkString(String string, Integer n) {
String regex = "^.{0," + n + "}$";
return Pattern.matches(regex, string);
}
}