import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
/**
* 正则验证工具
*
*/
public class RegularUtil {
/**
* 正则表达式:验证用户名
*/
public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,20}$";
/**
* 正则表达式:验证密码
*/
public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";
/**
* 正则表达式:验证手机号
*/
public static final String REGEX_MOBILE = "^((13[0-9])|(14[0-9])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
/**
* 正则表达式:验证邮箱
*/
public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
/**
* 正则表达式:验证汉字
*/
public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";
/**
* 正则表达式:验证身份证
*/
public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";
/**
* 正则表达式:验证URL
*/
public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
/**
* 正则表达式:验证IP地址
*/
public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
/**
* 校验用户名
*
* @param username
* @return 校验通过返回true,否则返回false
*/
public static boolean isUsername(String username) {
return Pattern.matches(REGEX_USERNAME, username);
}
/**
* 校验密码
*
* @param password
* @return 校验通过返回true,否则返回false
*/
public static boolean isPassword(String password) {
return Pattern.matches(REGEX_PASSWORD, password);
}
/**
* 校验手机号
*
* @param mobile
* @return 校验通过返回true,否则返回false
*/
public static boolean isMobile(String mobile) {
if(Help.isNull(mobile)){
return false;
}
return Pattern.matches(REGEX_MOBILE, mobile);
}
/**
* 校验邮箱
*
* @param email
* @return 校验通过返回true,否则返回false
*/
public static boolean isEmail(String email) {
if(Help.isNull(email)){
return false;
}
return Pattern.matches(REGEX_EMAIL, email);
}
/**
* 校验汉字
*
* @param chinese
* @return 校验通过返回true,否则返回false
*/
public static boolean isChinese(String chinese) {
return Pattern.matches(REGEX_CHINESE, chinese);
}
/**
* 校验身份证
*
* @param idCard
* @return 校验通过返回true,否则返回false
*/
public static boolean isIDCard(String idCard) {
return Pattern.matches(REGEX_ID_CARD, idCard);
}
/**
* 校验URL
*
* @param url
* @return 校验通过返回true,否则返回false
*/
public static boolean isUrl(String url) {
return Pattern.matches(REGEX_URL, url);
}
/**
* 校验IP地址
*
* @param ipAddr
* @return
*/
public static boolean isIPAddr(String ipAddr) {
return Pattern.matches(REGEX_IP_ADDR, ipAddr);
}
/**
* 营业执照 统一社会信用代码(18位)
* @param license
* @return
*/
public static boolean isLicense18(String businessCode) {
if(Help.isNull(businessCode)) {
return false;
}
if(businessCode.length() != 18) {
return false;
}
String baseCode = "0123456789ABCDEFGHJKLMNPQRTUWXY";
char[] baseCodeArray = baseCode.toCharArray();
Map<Character, Integer> codes = new HashMap<Character, Integer>();
for (int i = 0; i < baseCode.length(); i++) {
codes.put(baseCodeArray[i], i);
}
char[] businessCodeArray = businessCode.toCharArray();
Character check = businessCodeArray[17];
if (baseCode.indexOf(check) == -1) {
return false;
}
int[] wi = { 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28 };
int sum = 0;
for (int i = 0; i < 17; i++) {
Character key = businessCodeArray[i];
if (baseCode.indexOf(key) == -1) {
return false;
}
sum += (codes.get(key) * wi[i]);
}
int value = 31 - sum % 31;
return value == codes.get(check);
}
}