JAVA之校验工具类
整理了一些java的正则校验工具类
package com.gccloud.idc.common.util;
import com.alibaba.cloud.commons.lang.StringUtils;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Java表单验证工具类
*
* @author
*
*/public class RegexUtil {
public static void main(String[] args) {
// System.out.println("过滤中英文特殊字符: "+RegexUtil.stringFilter("中国aaa123~~!#$%%."));
// System.out.println("过滤中英文特殊字符: "+RegexUtil.numberFilter("123124asdafa123412"));
// System.out.println("是否包含中英文特殊字符: "+RegexUtil.isContainsSpecialChar("12"));
// System.out.println("判断是否为时间格式: "+RegexUtil.isDate("2014-01-31 18:00:00"));
// System.out.println("判断是否为日期格式: "+RegexUtil.isDateT("2014-01-31"));
// System.out.println("过滤html代码: "+RegexUtil.htmltoText("<JAVASCRIPT>12</JAVASCRIPT>DDDDD"));
// System.out.println("判断中文字符: "+RegexUtil.isChineseChar("中国!"));
// System.out.println("匹配汉字: "+RegexUtil.isChinese("中国!"));
// System.out.println("判断英文字符: "+RegexUtil.isEnglish("abc!"));
// System.out.println("判断合法字符: "+RegexUtil.isRightfulString("abc_-11AAA"));
// System.out.println("邮政编码验证: "+RegexUtil.isZipCode("162406"));
// System.out.println("身份证号码验证: "+RegexUtil.isIdCardNo("35052419880210133e"));
// System.out.println("手机号码验证: "+RegexUtil.isMobile("1786876537"));
// System.out.println("::::>>>"+RegexUtil.numberFilter("1"));
// System.out.println("电话号码验证: "+RegexUtil.isPhone("091347943459"));
// System.out.println("验证中国大陆手机号和座机号: "+RegexUtil.checkPhoneAndCall("02912345"));
// System.out.println("匹配密码: "+RegexUtil.isPwd("d888d_ddddd"));
// System.out.println("匹配密码: "+RegexUtil.isUrl("http://baidu.com"));
// System.out.println("验证字符: "+RegexUtil.stringCheck("中文aabc001_-"));
// System.out.println("验证正整数: "+RegexUtil.isInteger("1"));
// System.out.println("验证特殊字符: "+RegexUtil.isContainsSpecialChar("1"));
// System.out.println(isEmail("416501600@qq.com"));
//http://baidu.com www.baidu.com baidu.com// System.out.println(NumberUtils.toInt("-0000000002"));
}
/**
* 中国电信号码格式验证 手机段: 133,149,153,173,177,180,181,189,191,199,1349,1410,1700,1701,1702
**/ private static final String CHINA_TELECOM_PATTERN = "(?:^(?:\\+86)?1(?:33|49|53|7[37]|8[019]|9[19])\\d{8}$)|(?:^(?:\\+86)?1349\\d{7}$)|(?:^(?:\\+86)?1410\\d{7}$)|(?:^(?:\\+86)?170[0-2]\\d{7}$)";
/**
* 中国联通号码格式验证 手机段:130,131,132,145,146,155,156,166,171,175,176,185,186,1704,1707,1708,1709
**/ private static final String CHINA_UNICOM_PATTERN = "(?:^(?:\\+86)?1(?:3[0-2]|4[56]|5[56]|66|7[156]|8[56])\\d{8}$)|(?:^(?:\\+86)?170[47-9]\\d{7}$)";
/**
* 中国移动号码格式验证
* 手机段:134,135,136,137,138,139,147,148,150,151,152,157,158,159,172,178,182,183,184,187,188,195,198,1440,1703,1705,1706
**/ private static final String CHINA_MOBILE_PATTERN = "(?:^(?:\\+86)?1(?:3[4-9]|4[78]|5[0-27-9]|72|78|8[2-478]|98|95)\\d{8}$)|(?:^(?:\\+86)?1440\\d{7}$)|(?:^(?:\\+86)?170[356]\\d{7}$)";
private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat formatterDate = new SimpleDateFormat("yyyy-MM-dd");
public final static boolean isNull(Object[] objs){
if(objs==null||objs.length==0) return true;
return false;
}
public final static boolean isNull(Integer integer){
if(integer==null||integer==0) return true;
return false;
}
public final static boolean isNull(Collection collection){
if(collection==null||collection.size()==0) return true;
return false;
}
public final static boolean isNull(Map map){
if(map==null||map.size()==0) return true;
return false;
}
public final static boolean isNull(String str){
return str == null || "".equals(str.trim()) || "null".equals(str.toLowerCase());
}
public final static boolean isNull(Long longs){
if(longs==null||longs==0) return true;
return false;
}
public final static boolean isNotNull(Long longs){
return !isNull(longs);
}
public final static boolean isNotNull(String str){
return !isNull(str);
}
public final static boolean isNotNull(Collection collection){
return !isNull(collection);
}
public final static boolean isNotNull(Map map){
return !isNull(map);
}
public final static boolean isNotNull(Integer integer){
return !isNull(integer);
}
public final static boolean isNotNull(Object[] objs){
return !isNull(objs);
}
/**
* 匹配URL地址
*
* @param str
* @return
* @author jiqinlin
*/ public final static boolean isUrl(String str) {
return match(str, "^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$");
}
/**
* 匹配密码,以字母开头,长度在6-12之间,只能包含字符、数字和下划线。
*
* @param str
* @return
* @author jiqinlin
*/ public final static boolean isPwd(String str) {
return match(str, "^[a-zA-Z]\\w{6,12}$");
}
/**
* 验证字符,只能包含中文、英文、数字、下划线等字符。
*
* @param str
* @return
* @author jiqinlin
*/ public final static boolean stringCheck(String str) {
return match(str, "^[a-zA-Z0-9\u4e00-\u9fa5-_]+$");
}
/**
* 匹配Email地址
*
* @param str
* @return
* @author jiqinlin
*/ public final static boolean isEmail(String str) {
return match(str, "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
}
/**
* 匹配非负整数(正整数+0)
*
* @param str
* @return
* @author jiqinlin
*/ public final static boolean isInteger(String str) {
return match(str, "^[+]?\\d+$");
}
/**
* 判断数值类型,包括整数和浮点数
*
* @param str
* @return
* @author jiqinlin
*/ public final static boolean isNumeric(String str) {
if(isFloat(str) || isInteger(str)) return true;
return false;
}
/**
* 只能输入数字
*
* @param str
* @return
* @author jiqinlin
*/ public final static boolean isDigits(String str) {
return match(str, "^[0-9]*$");
}
/**
* 匹配正浮点数
*
* @param str
* @return
* @author jiqinlin
*/ public final static boolean isFloat(String str) {
return match(str, "^[-\\+]?\\d+(\\.\\d+)?$");
}
/**
* 联系电话(手机/电话皆可)验证
*
* @param text
* @return
* @author jiqinlin
*/ public final static boolean isTel(String text){
if(isMobile(text)||isPhone(text)) return true;
return false;
}
/**
* 电话号码验证
*
* @param text
* @return
* @author jiqinlin
*/ public final static boolean isPhone(String text){
return match(text, "^(\\d{3,4}-?)?\\d{7,9}$");
}
/**
* 手机号码验证
*
* @param text
* @return
* @author jiqinlin
*/ // 暂时不可用
public final static boolean isMobile(String text){
if(text.length()!=11) return false;
return match(text, "^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\\d{8})$");
}
/**
* 身份证号码验证
*
* @param text
* @return
* @author jiqinlin
*/ public final static boolean isIdCardNo(String text){
return match(text, "^(\\d{6})()?(\\d{4})(\\d{2})(\\d{2})(\\d{3})(\\w)$");
}
/**
* 邮政编码验证
*
* @param text
* @return
* @author jiqinlin
*/ public final static boolean isZipCode(String text){
return match(text, "^[0-9]{6}$");
}
/**
* 判断整数num是否等于0
* * @param num
* @return
* @author jiqinlin
*/ public final static boolean isIntEqZero(int num){
return num==0;
}
/**
* 判断整数num是否大于0
* * @param num
* @return
* @author jiqinlin
*/ public final static boolean isIntGtZero(int num){
return num>0;
}
/**
* 判断整数num是否大于或等于0
* * @param num
* @return
* @author jiqinlin
*/ public final static boolean isIntGteZero(int num){
return num>=0;
}
/**
* 判断浮点数num是否等于0
* * @param num 浮点数
* @return
* @author jiqinlin
*/ public final static boolean isFloatEqZero(float num){
return num==0f;
}
/**
* 判断浮点数num是否大于0
* * @param num 浮点数
* @return
* @author jiqinlin
*/ public final static boolean isFloatGtZero(float num){
return num>0f;
}
/**
* 判断浮点数num是否大于或等于0
* * @param num 浮点数
* @return
* @author jiqinlin
*/ public final static boolean isFloatGteZero(float num){
return num>=0f;
}
/**
* 判断是否为合法字符(a-zA-Z0-9-_)
* * @param text
* @return
* @author jiqinlin
*/ public final static boolean isRightfulString(String text){
return match(text, "^[A-Za-z0-9_-]+$");
}
/**
* 判断英文字符(a-zA-Z)
* * @param text
* @return
* @author jiqinlin
*/ public final static boolean isEnglish(String text){
return match(text, "^[A-Za-z]+$");
}
/**
* 判断中文字符(包括汉字和符号)
* * @param text
* @return
* @author jiqinlin
*/ public final static boolean isChineseChar(String text){
return match(text, "^[\u0391-\uFFE5]+$");
}
/**
* 匹配汉字
*
* @param text
* @return
* @author jiqinlin
*/ public final static boolean isChinese(String text){
return match(text, "^[\u4e00-\u9fa5]+$");
}
/**
* 判断是否为正确的时间格式
*
* @param text
* @return
* @author
*/
public final static boolean isDate(String text){
return match(text, "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
}
/**
* 判断是否为正确的日期格式
*
* @param text
* @return
* @author
*/
public final static boolean isDateT(String text){
return match(text, "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))");
}
/**
* 是否包含中英文特殊字符,除英文"-_"字符外
*
* @param text
* @return
*/
public static boolean isContainsSpecialChar(String text) {
if(StringUtils.isBlank(text)) return false;
String[] chars={"[","`","~","!","@","#","$","%","^","&","*","(",")","+","=","|","{","}","'",
":",";","'",",","[","]",".","<",">","/","?","~","!","@","#","¥","%","…","&","*","(",")",
"—","+","|","{","}","【","】","‘",";",":","”","“","’","。",",","、","?","]"};
for(String ch : chars){
if(text.contains(ch)) return true;
}
return false;
}
/**
* 过滤中英文特殊字符,除英文"-_"字符外
*
* @param text
* @return
*/
public static String stringFilter(String text) {
String regExpr="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
Pattern p = Pattern.compile(regExpr);
Matcher m = p.matcher(text);
return m.replaceAll("").trim();
}
/**
* 仅保留数字
*
* @param text
* @return
*/
public static String numberFilter(String text) {
if(text != null){
String regExpr= "[^0-9]";
Pattern p = Pattern.compile(regExpr);
Matcher m = p.matcher(text);
return m.replaceAll("").trim();
}
return null;
}
/**
* 仅保留数字和第一个小数点
*
* @param text
* @return Double
*/ public static Double numberFilterDouble(String text) {
if(text != null){
String regExpr = "[^0-9.]"; // 匹配非数字和非小数点的字符
Pattern p = Pattern.compile(regExpr);
Matcher m = p.matcher(text);
String trim = m.replaceAll("").trim(); // 移除非数字和非小数点的字符
int firstDotIndex = trim.indexOf('.'); // 获取第一个小数点的索引
if (firstDotIndex != -1) {
// 如果存在小数点,则保留第一个小数点,移除其他小数点
trim = trim.substring(0, firstDotIndex + 1) + trim.substring(firstDotIndex + 1).replace(".", "");
}
double v = Double.parseDouble(trim); // 将字符串转换为double类型
String format = String.format("%.2f", v); // 格式化为保留两位小数的字符串
return Double.parseDouble(format); // 将格式化后的字符串转换为double类型并返回
}
return null;
}
/**
* * 判断当前值是否合格
* 如果基准值大于参考值,则 当前值 大于 参考值为合格 反之不合格
* 如果基准值小于参考值,则 当前值 小于 参考值为合格 反之不合格
* @param
* indexValue baseValue referenceValue
* * @return boolean
* */ public static boolean isItStandard(double indexValue,double baseValue,double referenceValue) {
// 返回值 是否合格
boolean isItStandard = false;
// 默认 值比基准值 大
boolean isBig = false;
if (baseValue <= referenceValue) {
isBig = true;
}
if(isBig){
if(indexValue <= baseValue){
isItStandard = true;
}
}else{
if(indexValue >= baseValue){
isItStandard = true;
}
}
return isItStandard;
}
/**
* 过滤html代码
*
* @param inputString 含html标签的字符串
* @return
*/
public static String htmlFilter(String inputString) {
String htmlStr = inputString; // 含html标签的字符串
String textStr = "";
java.util.regex.Pattern p_script;
java.util.regex.Matcher m_script;
java.util.regex.Pattern p_style;
java.util.regex.Matcher m_style;
java.util.regex.Pattern p_html;
java.util.regex.Matcher m_html;
java.util.regex.Pattern p_ba;
java.util.regex.Matcher m_ba;
try {
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
// } String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
// } String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
String patternStr = "\\s+";
p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签
p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤style标签
p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签
p_ba = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
m_ba = p_ba.matcher(htmlStr);
htmlStr = m_ba.replaceAll(""); // 过滤空格
textStr = htmlStr;
} catch (Exception e) {
System.err.println("Html2Text: " + e.getMessage());
}
return textStr;// 返回文本字符串
}
/**
* 正则表达式匹配
*
* @param text 待匹配的文本
* @param reg 正则表达式
* @return
* @author jiqinlin
*/ private final static boolean match(String text, String reg) {
if (StringUtils.isBlank(text) || StringUtils.isBlank(reg))
return false;
return Pattern.compile(reg).matcher(text).matches();
}
/**
* 验证中国大陆手机号和座机号
*
* @param phone
*
* @return
*/
public static boolean checkPhoneAndCall(String phone) {
if (StringUtils.isNotBlank(phone)) {
if (checkChinaMobile(phone) || checkChinaUnicom(phone) || checkChinaTelecom(phone) || isPhone(phone)) {
return true;
}
}
return false;
}
/**
* 中国大陆手机号码校验
*
* @param phone
*
* @return
*/
public static boolean checkPhone(String phone) {
if (StringUtils.isNotBlank(phone)) {
if (checkChinaMobile(phone) || checkChinaUnicom(phone) || checkChinaTelecom(phone)) {
return true;
}
}
return false;
}
/**
* 中国移动手机号码校验
*
* @param phone
*
* @return
*/
public static boolean checkChinaMobile(String phone) {
if (StringUtils.isNotBlank(phone)) {
Pattern regexp = Pattern.compile(CHINA_MOBILE_PATTERN);
if (regexp.matcher(phone).matches()) {
return true;
}
}
return false;
}
/**
* 中国联通手机号码校验
*
* @param phone
*
* @return
*/
public static boolean checkChinaUnicom(String phone) {
if (StringUtils.isNotBlank(phone)) {
Pattern regexp = Pattern.compile(CHINA_UNICOM_PATTERN);
if (regexp.matcher(phone).matches()) {
return true;
}
}
return false;
}
/**
* 中国电信手机号码校验
*
* @param phone
*
* @return
*/
public static boolean checkChinaTelecom(String phone) {
if (StringUtils.isNotBlank(phone)) {
Pattern regexp = Pattern.compile(CHINA_TELECOM_PATTERN);
if (regexp.matcher(phone).matches()) {
return true;
}
}
return false;
}
/**
* 隐藏手机号中间四位
*
* @param phone
*
* @return java.lang.String
*/ public static String hideMiddleMobile(String phone) {
if (StringUtils.isNotBlank(phone)) {
phone = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
}
return phone;
}
/**
* 日期加一
*
* @param dateTime 需要转换的时间
* @param num 加 减 1
* @param isDate 是否是日期(1,是 0,否)
*
* @return java.lang.String
*/ public static String dateAddOne(String dateTime,Integer num,Integer isDate) throws Exception{
if (dateTime == null) {
dateTime = formatter.format(new Date());
}
if (num == null) {
num = 1;
}
Date date = new Date();
String format = null;
Calendar calendar = new GregorianCalendar();
if (isDate == 1) {
date = formatterDate.parse(dateTime);
calendar.setTime(date);
calendar.add(calendar.DATE,num); //把日期往后增加一天,整数 往后推,负数往前移动
date=calendar.getTime(); //这个时间就是日期往后推一天的结果
format = formatterDate.format(date);
}else {
date = formatter.parse(dateTime);
calendar.setTime(date);
calendar.add(calendar.DATE,num); //把日期往后增加一天,整数 往后推,负数往前移动
date=calendar.getTime(); //这个时间就是日期往后推一天的结果
format = formatter.format(date);
}
return format;
}
}