RegUtil工具类 3.0.2

package com.jfai.kg.kafkacomsu.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author wanglf
 * @version 3.0.2
 * @deprecated:字符串正则验证类
 * @Time:2018年7月2日11:44:55
 * @remark  更新了字符串校验传入空值,会报NullPointException的问题
 * 增强了空值校验
 */
public class RegUtil {


public static boolean isEmail(String email) {
if (email==null||email.length()==0||"".equals(email.trim())) {return false;}
Pattern p = Pattern.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
Matcher m = p.matcher(email);
return m.matches();
}


/**
* 验证是否是手机号码
* @param mobileNum
* @return true 是 false 不是
* @remark 新增16开头的号段
*/
public static boolean isMobileNO(String mobileNum) {
if (mobileNum==null||mobileNum.length()==0||"".equals(mobileNum.trim())) {return false;}
Pattern p = Pattern.compile("^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$");
Matcher m = p.matcher(mobileNum);
return m.matches();
}


/**
* 验证是否是手机号码
* @param Mobile phone Number!
* @return true 是 false 不是
* @remark 新增16开头的号段
*/
public static boolean isPhone(String phone) {
if (phone==null||phone.length()==0||"".equals(phone.trim())) {return false;}
return isMobileNO(phone);
}


/**
* 验证是否是手机号码
* @param 固话电话号 
* @return
*/
public static boolean isFixPhone(String phoneNum) {
if (phoneNum==null||phoneNum.length()==0||"".equals(phoneNum.trim())) {return false;}
String reg = "(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)|" + "(?:(86-?)?(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)";
return match(reg, phoneNum);
}


/**
* 验证IPv4
* @param isIPv4
* @return
*/
public static boolean isIPv4(String IPv4) {
if (IPv4==null||IPv4.length()==0||"".equals(IPv4.trim())) {return false;}
String reg = "\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";
return match(reg, IPv4);
}


/**
* 验证IPv6
* @param isIPv6
* @return
*/
public static boolean isIPv6(String IPv6) {
if (IPv6==null||IPv6.length()==0||"".equals(IPv6.trim())) {return false;}
String reg = "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))";
return match(reg, IPv6);
}


/**
* 验证邮编
* @param postCode
* @return
*/
public static boolean isPostCode(String postCode) {
if (postCode==null||postCode.length()==0||"".equals(postCode.trim())) {return false;}
String reg = "[1-9]\\d{5}";
return match(reg, postCode);
}


/**
* 验证是否是用户名
* 以字母开头,4-20位大小写数字或下划线组成
* @param userName str
* @return true 符合规定 false 不符合规定
*/
public static boolean checkUserName(String userName) {
if (userName==null||userName.length()==0||"".equals(userName.trim())) {return false;}
Pattern p = Pattern.compile("^[a-zA-Z]+([a-zA-Z0-9]|[_]){2,20}$");
Matcher m = p.matcher(userName);
return m.matches();
}


/**
* 验证是否是userid  1-19位的正整数   最大 :9223372036854775807  bigint(20)  java Long MAX_VALUE
* @param userId 
* @return true 是 false 不是
*/
public static boolean isUserId(String userId) {
if (userId==null||userId.length()==0||"".equals(userId.trim())) {return false;}
Pattern p = Pattern.compile("[1-9][0-9]{0,18}");
Matcher m = p.matcher(userId);
return m.matches();
}


/**
* 验证密码是否是6-16位
* 格式为:6-16位,字母区分大小写
* @param password
* @return
*/
public static boolean isPassWord(String password) {
if (password==null||password.length()==0||"".equals(password.trim())) {return false;}
// Pattern p = Pattern.compile("^[0-9a-zA-Z\\.\\_]{6,20}$");
// Matcher m = p.matcher(password);
// logger.info(m.matches()+"---");
int len = password.length();
boolean bol = true;
if (len < 6 || len > 16) {
bol = false;
}
return bol;
}


/**
* 验证是否是手机验证码
* 6位数字
* @param password
* @return
*/
public static boolean isPhoneProving(String phoneProving) {
if (phoneProving==null||phoneProving.length()==0||"".equals(phoneProving.trim())) {return false;}
Pattern p = Pattern.compile("^[0-9]{6}$");
Matcher m = p.matcher(phoneProving);
return m.matches();
}


/**
* 验证字符串是否是日期
* @param dateStr  
* @return
* @remark 经测试只有yyyy-MM-dd 和yyyyMMdd格式通过,yyyy-MM-dd HH:mm:ss格式不通过
* @result Abandon(弃用)
*/
@Deprecated
public static boolean isDate(String dateStr) {
if (dateStr==null||dateStr.length()==0||"".equals(dateStr.trim())) {return false;}
String reg = "^((\\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-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
Pattern pattern = Pattern.compile(reg);
Matcher m = pattern.matcher(dateStr);
if (m.matches()) {
return true;
} else {
return false;
}
}


/**

* @param str 日期字符串
* @param formatStr  日期格式化字符串
* @return
* @Description: 验证日期字符串合法性
*/
public static boolean validDateStr(String str, String formatStr) {
if (formatStr==null||formatStr.length()==0||"".equals(formatStr.trim())) {return false;}
if (formatStr==null||str.length()==0||"".equals(str.trim())) {return false;}
boolean convertSuccess = true;
SimpleDateFormat format = new SimpleDateFormat(formatStr);
try {
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
// e.printStackTrace();
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
convertSuccess = false;
}
return convertSuccess;
}


/**
* 验证字符串是否是正整数
* @param str
* @return
*/
public static boolean isNumber(String str) {
if (str==null||str.length()==0||"".equals(str.trim())) {return false;}
Pattern p = Pattern.compile("^[0-9]+$");
Matcher m = p.matcher(str);
return m.matches();
}


/**
* 验证字符串是否是IMEI
* @param str
* @return
*/
public static boolean isIMEI(String str) {
if (str==null||str.length()==0||"".equals(str.trim())) {return false;}
Pattern p = Pattern.compile("^[0-9]{15}$");
Matcher m = p.matcher(str);
return m.matches();
}


/**
* 验证字符串是否是URL
* @param str
* @return
*/
public static boolean isUrl(String str) {
if (str==null||str.length()==0||"".equals(str.trim())) {return false;}
String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
return m.matches();
}


/**
* 验证身份证ID
* @param str
* @return
*/
public static boolean isIDNo(String str) {
return IDNoValidate.isIDCard(str);
// if (str==null||str.length()==0||"".equals(str.trim())) {return false;}
// String regex = "^(\\d{16}|\\d{18}|\\d{17}(X|x))$";
// return match(regex, str);
}


/**
* 验证月
* @param str
* @return
*/
public static boolean isMonth(String str) {
if (str==null||str.length()==0||"".equals(str.trim())) {return false;}
String regex = "^(0?[[1-9]|1[0-2])$";
return match(regex, str);
}


/**
* 验证天数
* @param str
* @return
*/
public static boolean isDay(String str) {
if (str==null||str.length()==0||"".equals(str.trim())) {return false;}
String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$";
return match(regex, str);
}


/**
* 大写字符串
* @param str
* @return
*/
public static boolean isUpString(String str) {
if (str==null||str.length()==0||"".equals(str.trim())) {return false;}
String regex = "^[A-Z]+$";
return match(regex, str);
}


/**
* 小写字符串
* @param str
* @return
*/
public static boolean isLowString(String str) {
if (str==null||str.length()==0||"".equals(str.trim())) {return false;}
String regex = "^[a-z]+$";
return match(regex, str);
}


/**
* 汉字
* @param str
* @return
*/
public static boolean isChinese(String str) {
if (str==null||str.length()==0||"".equals(str.trim())) {return false;}
String regex = "^[\u4e00-\u9fa5]+$";
return match(regex, str);
}


/**
* 中国名字
* @param str
* @return
*/
public static boolean isChineseName(String str) {
if (str==null||str.length()==0||"".equals(str.trim())) {return false;}
String regex = "^[\u4e00-\u9fa5|\uf900-\ufa2d|·]{2,20}$"; // · ==>Unicode 00B7
return match(regex, str);
}


/**
* 银行卡号
* @param str
* @return
* @describetion  包括信用卡(16位)和借记卡(19位)
* @remark 注意 工商银行有15位卡号的信用卡
*/
public static boolean isBankCardNo(String str) {
if (str==null||str.length()==0||"".equals(str.trim())) {return false;}
String reg = "^([1-9]{1})(\\d{15}|\\d{18}|\\d{14})$";
return match(reg, str);
}


public static boolean match(String regex, String str) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
return m.matches();
// reg =\\< *[img][^\\\\>]*[src] *= *[\\"\\']{0,1}([^\\"\\'\\ >]*) 提取网页中的图片信息
// reg=(<a\\s*(?!.*\\brel=)[^>]*)(href="https?:\\/\\/)((?!(?:(?:www\\.)?'.implode('|(?:www\\.)?',
// $follow_list).'))[^"]+)"((?!.*\\brel=)[^>]*)(?:[^>]*)> 提取html页面中的超链接
}


public static void main(String[] args) {
// System.out.println(isDate("2016-10-06"));
// System.out.println(isDate("2016-10-06 16:24:36"));
// System.out.println(isDate("2016-10-06 16:24:36:8"));
// System.out.println(isDate("2016-10-06 16:24:36:89"));
// System.out.println(isDate("2016-10-06 16:24:36:102"));
// System.out.println(isDate("20161006"));
// System.out.println(isDate("20161006162436"));


System.out.println(isDate("20120-10-06"));
System.out.println(isDate("2016-100-06"));
System.out.println(isDate("2016-10-81"));
System.out.println(isDate("100-10-01"));


// String FixedPhone = "0571-8888880-111";
// System.out.println(isFixPhone("010-89178263"));


// System.out.println(isNumber(" "));
// System.out.println(isChineseName("粑粑热烈·木木麦提"));
// System.out.println(RegUtil.isEmail("[email protected]"));
// System.out.println(RegUtil.isMobileNO("18866666666"));
// System.out.println(RegUtil.isUserName("a2_2ss号"));
// System.out.println(RegUtil.isNumber("0.22222"));
// System.out.println(RegUtil.isPhoneProving("222_22"));
// System.out.println(RegUtil.isIMEI("11111111111111"));
// String cardNo = StringUtil.getNum(16);
// System.out.println(cardNo);
// System.out.println(isCardNo(cardNo));
// System.out.println(isChineseName("王·木木麦提"));
// System.out.println(isIDCard("41041119921101559X"));
}


}


// 其他符号中,至少有四个可能被混淆用于中文姓名的居中圆点:「∙」「•」「・」「●」,分别叫做「Bullet
// Operator」、「Bullet」、「Katakana Middle Dot」、「Black Circle」

猜你喜欢

转载自blog.csdn.net/u014793936/article/details/80806201
今日推荐