import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 正则表达式工具类
*/
public class RegexUtil {
/**
* 判断是否为数字
*
* @param obj
* @return
*/
public static boolean isNumber(Object obj) {
if (obj != null) {
return isNumber(obj.toString());
}
return false;
}
/**
* 判断是否为数字
*
* @param str
* @return
*/
public static boolean isNumber(String str) {
if (str == null || str.length() > 300)
return false;
String regex = "^(-?[1-9]\\d*\\.?\\d*)|(-?0\\.\\d*[1-9])|(-?[0])|(-?[0]\\.\\d*)$";
return str.matches(regex);
}
/**
* 判断是否为整数
*
* @param obj
* @return
*/
public static boolean isInteger(Object obj) {
if (obj != null) {
return isInteger(obj.toString());
}
return false;
}
/**
* 判断是否为整数
*
* @param str
* @return
*/
public static boolean isInteger(String str) {
if (str == null)
return false;
String regex = "^[+-]?[0-9]{1,9}$";
return str.matches(regex);
}
/**
* 判断是否为带小数点的整数
*
* @param str
* @return
*/
public static boolean isIntegerWithComma(String str) {
if (str == null)
return false;
String regex = "^[+-]?([0-9]{1,9}|[0-9]{1,9}\\.0+)$";
return str.matches(regex);
}
/**
* 判断是否为长整数
*
* @param obj
* @return
*/
public static boolean isLong(Object obj) {
if (obj != null) {
return isInteger(obj.toString());
}
return false;
}
/**
* 判断是否为长整数
*
* @param str
* @return
*/
public static boolean isLong(String str) {
if (str == null)
return false;
String regex = "^[+-]?[0-9]{1,18}$";
return str.matches(regex);
}
/**
* 判断是否为带小数点的长整数
*
* @param str
* @return
*/
public static boolean isLongWithComma(String str) {
if (str == null)
return false;
String regex = "^[+-]?([0-9]{1,18}|[0-9]{1,18}\\.0+)$";
return str.matches(regex);
}
/**
* 判断是否为小数
*
* @param obj
* @return
*/
public static boolean isDecimal(Object obj) {
if (obj != null) {
return isInteger(obj.toString());
}
return false;
}
/**
* 判断是否为小数
*
* @param str
* @return
*/
public static boolean isDecimal(String str) {
if (str == null || str.length() > 300)
return false;
String regex = "^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$";
return str.matches(regex);
}
/**
* 判断是否为日期(只包含年月日)
*
* @param obj
* @return
*/
public static boolean isDate(Object obj) {
if (obj != null) {
return isDate(obj.toString());
}
return false;
}
/**
* 判断是否为日期(只包含年月日)
*
* @param str
* @return
*/
public static boolean isDate(String str) {
if (str == null)
return false;
String regex = "((^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(10|12|0?[13578])([-\\/\\._]?)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(11|0?[469])([-\\/\\._]?)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(0?2)([-\\/\\._]?)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([3579][26]00)([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([1][89][0][48])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][0][48])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([1][89][2468][048])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][2468][048])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([1][89][13579][26])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][13579][26])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$))";
return str.matches(regex);
}
/**
* 判断是否为日期时间(包含年月日时分秒)
*
* @param obj
* @return
*/
public static boolean isDateTime(Object obj) {
if (obj != null) {
return isDateTime(obj.toString());
}
return false;
}
/**
* 判断是否为日期时间(包含年月日时分秒)
*
* @param str
* @return
*/
public static boolean isDateTime(String str) {
if (str == null)
return false;
String regex = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)\\s+([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$";
return str.matches(regex);
}
/**
* 判断是否为邮箱地址
*
* @param obj
* @return
*/
public static boolean isMailAddress(Object obj) {
if (obj != null) {
return isMailAddress(obj.toString());
}
return false;
}
/**
* 判断是否为邮箱地址
*
* @param str
* @return
*/
public static boolean isMailAddress(String str) {
if (str == null)
return false;
String regex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
return str.matches(regex);
}
public static boolean isValidIdNo(String str) {
if (str == null)
return false;
String regex = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X|x)$";
return str.matches(regex);
}
/**
* 判断是否包含特殊字符
*
* @param str
* @return
*/
public static boolean containSpecialCharacter(String str) {
String regex = "[\\{\\}<>\\+\\*/=\\?!@#$%\\^&\\|\\\\]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
return m.find();
}
/**
* 判断是否包含中文字符
*
* @param str
* @return
*/
public static boolean containChinese(String str) {
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(str);
if (m.find()) {
return true;
}
return false;
}
/**
* 判断是否是正确的IP地址
*
* @param ip
* @return boolean true,通过,false,没通过
*/
public static boolean isIp(String ip) {
if (null == ip || "".equals(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);
}
/**
* 判断是否是正确的邮箱地址
*
* @param email
* @return boolean true,通过,false,没通过
*/
public static boolean isEmail(String email) {
if (null == email || "".equals(email))
return false;
String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
return email.matches(regex);
}
/**
* 判断是否含有中文,仅适合中国汉字,不包括标点
* @param text
* @return boolean true,通过,false,没通过
*/
public static boolean isChinese(String text) {
if (null == text || "".equals(text))
return false;
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(text);
return m.find();
}
/**
* 判断几位小数(正数)
*
* @param decimal
* 数字
* @param count
* 小数位数
* @return boolean true,通过,false,没通过
*/
public static boolean isDecimal(String decimal, int count) {
if (null == decimal || "".equals(decimal))
return false;
String regex = "^(-)?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){" + count
+ "})?$";
return decimal.matches(regex);
}
/**
* 判断是否是手机号码
*
* @param phoneNumber
* 手机号码
* @return boolean true,通过,false,没通过
*/
public static boolean isPhoneNumber(String phoneNumber) {
if (null == phoneNumber || "".equals(phoneNumber))
return false;
String regex = "^1[3|4|5|8][0-9]\\d{8}$";
return phoneNumber.matches(regex);
}
/**
* 判断是否含有特殊字符
*
* @param text
* @return boolean true,通过,false,没通过
*/
public static boolean hasSpecialChar(String text) {
if (null == text || "".equals(text))
return false;
if (text.replaceAll("[a-z]*[A-Z]*\\d*-*_*\\s*", "").length() == 0) {
// 如果不包含特殊字符
return true;
}
return false;
}
/**
* 适应CJK(中日韩)字符集,部分中日韩的字是一样的
*/
public static boolean isChinese2(String strName) {
char[] ch = strName.toCharArray();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (isChinese(c)) {
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;
}
/**
* 判断身份证合法性
* @param personIDCode
* @return
*/
public static boolean isPersonIDCode(String personIDCode) {
if (null == personIDCode || "".equals(personIDCode))
return false;
Pattern idNumPattern = Pattern.compile("(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])");
Matcher idNum = idNumPattern.matcher(personIDCode);
return idNum.matches() ;
}
/**
* @Description: 判断字符串是不是数字
* @param @param str
* @param @return
* @return boolean
*/
public static boolean isNumberByBigDecimal(String str) {
BigDecimal result = null;
try {
result = new BigDecimal(str);
} catch (Exception e) {
}
if (result == null) {
return false;
} else {
return true;
}
}
}