工作需要,再次复习巩固下正则表达式。
package com.bris.util;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
/**
* <p>
* Description: 参数校验相关的工具类
* </p>
*
* @Author zhangzhenxiao
* @Version V1.0
*/
public class ValidateUtil {
private static final int bit_of_chinese = 3;
public static boolean checkLengthMax(String s, int maxlen) {
int len = getStrLength(s);
return len <= maxlen;
}
public static boolean checkLengthMin(String s, int minlen) {
int len = getStrLength(s);
return len >= minlen;
}
public static boolean checkLength(String s, int minlen, int maxlen) {
int len = getStrLength(s);
return len >= minlen && len <= maxlen;
}
public static boolean checkLen(String s, int len) {
if (s == null || s.trim().equals("")) {
return true;
}
else {
int l = getStrLength(s);
if (l <= len)
return true;
else
return false;
}
}
public static boolean checkInt(String s, int std) {
if (s == null || s.trim().equals(""))
return true;
else
s = s.trim();
try {
long val = Long.parseLong(s);
String vals = Long.toString(val);
if (!vals.equals(s))
return false;
else {
if (std > 0) {
if (val <= 0) return false;
}
else if (std == 0) {
if (val < 0) return false;
}
}
return true;
}
catch (Exception e) {
return false;
}
}
public static boolean checkFloat(String s, int std) {
if (s == null || s.trim().equals(""))
return true;
else
s = s.trim();
try {
double val = Double.parseDouble(s);
if (std > 0) {
if (val <= 0) return false;
}
else if (std == 0) {
if (val < 0) return false;
}
return true;
}
catch (Exception e) {
return false;
}
}
public static boolean checkDate(String s) {
return DateUtil.isValidDate(s);
}
public static boolean checkEmail(String s) {
// return Pattern
// .compile(
// "^[0-9a-zA-Z][a-zA-Z0-9\\.\\-_]{1,255}@[a-zA-Z0-9\\-]{1,255}[a-zA-Z0-9]\\.[a-zA-Z\\.]{1,255}[a-zA-Z]$")
// .matcher(s).matches();
return Pattern
.compile(
"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")
.matcher(s).matches();
}
public static boolean containChars(String s, char[] chars) {
if (s == null || s.trim().equals("")) return false;
if (chars == null || chars.length <= 0) return false;
for (int i = 0; i < chars.length; i++) {
if (s.indexOf(chars[i]) >= 0) return true;
}
return false;
}
private static int getStrLength(String s) {
if (s == null || s.trim().equals("")) {
return 0;
}
s = s.trim();
int l = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) > 255)
l = l + bit_of_chinese;
else
l = l + 1;
}
return l;
}
/*
* public static boolean validDate(String s) { if (s == null ||
* s.trim().equals("")) { return false; } else { s = s.trim();
* java.util.Date date = DateUtil.parseStringToDate(s); if (date == null)
* return false; else return true; } }
*
* public static boolean validTime(String s) { if (s == null ||
* s.trim().equals("")) { return false; } else { s = s.trim();
* java.util.Date date = DateUtil.parseStringToTime(s); if (date == null)
* return false; else return true; } }
*/
public static boolean isNum(String s) {
try {
Double.valueOf(s);
return true;
}
catch (NumberFormatException ex) {
return false;
}
}
public static boolean isIP(String str) {
Pattern p = Pattern
.compile("^([01]?[0-9][0-9]|[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9][0-9]|[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9][0-9]|[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9][0-9]|[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$");
Matcher m = p.matcher(str);
if (m.matches()) {
int loc = str.indexOf(".");
if (loc > 0) {
String strs = str;
String s = null;
while (loc > 0) {
s = strs.substring(0, loc);
try {
int val = Integer.parseInt(s);
if (val < 0 || val > 255) return false;
}
catch (Exception e) {
return false;
}
strs = strs.substring(loc + 1);
loc = strs.indexOf(".");
}
try {
int val = Integer.parseInt(strs);
if (val < 0 || val > 255) return false;
}
catch (Exception e) {
return false;
}
}
else {
try {
int val = Integer.parseInt(str);
if (val < 0 || val > 255) return false;
}
catch (Exception e) {
return false;
}
}
}
else {
return false;
}
return true;
}
/**
* 验证国内电话号码 格式:6767676, 号码位数必须是7-8位,头一位不能是"0"
*/
public static boolean checkPhoneNrWithoutCode(String phoneNr) {
String reg = "^[1-9]\\d{6,7}";
return Pattern.compile(reg).matcher(phoneNr).matches();
}
/**
* 验证邮编
*/
public static boolean checkPostcode(String postCode) {
String regex = "^[1-9]\\d{5}";
return Pattern.compile(regex).matcher(postCode).matches();
}
/**
* 验证国内电话号码 格式:010-67676767,区号长度3-4位,必须以"0"开头,号码是7-8位
*/
public static boolean checkPhoneNr(String phoneNr) {
String regex = "^[0]\\d{2,3}\\-\\d{7,8}";
return Pattern.compile(regex).matcher(phoneNr).matches();
}
/**
* checkByRegex 方法描述: 根据正则表达式校验输入 逻辑描述:
*
* @param sourceStr
* @param regex
* @return T/F
* @since Ver 1.00
*/
public static boolean checkByRegex(String sourceStr, String regex) {
if (sourceStr == null) {
return false;
}
boolean b = false;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(sourceStr);
while (matcher.matches()) {
return true;
}
return b;
}
/**
* 匹配由数字和26个英文字母组成的字符串
*/
private static final String letter_number_regexp = "^[A-Za-z0-9]+$";
public static String[] split(String input, String comma) {
return StringUtils.split(input, comma);
}
public static Map<String, String> parseData2Map(String data, String comma) {
String[] arr_data = split(data, comma);
if (arr_data.length % 2 != 0) {
return null;
}
Map<String, String> dataMap = new HashMap<String, String>();
for (int i = 0; i < arr_data.length; i += 2) {
dataMap.put(arr_data[i].toLowerCase().trim(), arr_data[i + 1]);
}
return dataMap;
}
public static boolean isBlank(String input) {
return StringUtils.isBlank(input);
}
public static String formatCode(String code) {
char[] strSeq = code.toUpperCase().toCharArray();
for (int i = 0; i < strSeq.length; i++) {
int ch = (int) strSeq[i];
if (ch >= 65 && ch <= 90) {
ch = ch - (((ch % 65) / 10 + 1) * 10 + 7);
strSeq[i] = (char) ch;
}
}
return new String(strSeq);
}
public static boolean checkSerialIn(String serialIn) {
if (StringUtils.isBlank(serialIn)) {
return false;
}
// 请求的流水号
Matcher isSerialIn = Pattern.compile("\\w{5,36}").matcher(serialIn);
if (isSerialIn.matches() && !"00000".equals(serialIn)) {
return true;
}
return false;
}
/**
* 校验手机号
*
* @param mobile
* @return
*/
public static boolean checkChinaMobileID(String mobile) {
if (StringUtils.isBlank(mobile)) {
return false;
}
if (mobile.length()!=11){
return false;
}
// if (mobile.length() > 20) {
// return false;
// }
// return Pattern.compile("\\d{1,15}").matcher(mobile).matches();
return true;
}
/**
* 校验APPCODE
*
* @param appID
* @return
*/
public static boolean checkAppCode(String appId) {
if (StringUtils.isBlank(appId)) {
return false;
}
// AppID 必须是3-5位的数字或字母
Matcher isAppID = Pattern.compile("\\w{3,5}").matcher(appId);//modify by gehj at 2013-11-15
if (isAppID.matches()) {
return true;
}
return false;
}
/**
* 校验签约发放证书时的RegistAPPCODE
*
* @param appID
* @return
*/
public static boolean checkRegistAppCode(String appId) {
if (StringUtils.isBlank(appId)) {
return false;
}
// AppID 必须是3-5位的数字
Matcher isAppID = Pattern.compile("\\w{3,5}").matcher(appId);
if (isAppID.matches()) {
return true;
}
return false;
}
/**
* 校验APPID
*
* @param appID
* @return
*/
public static boolean checkAppId(String appId) {
if (StringUtils.isBlank(appId)) {
return false;
}
// AppID 必须是3-5位的数字
Matcher isAppID = Pattern.compile("\\d{3,5}").matcher(appId);
if (isAppID.matches()) {
return true;
}
return false;
}
/**
* 校验APPID
*
* @param appID
* @return
*/
public static boolean checkPan(String pan) {
if (StringUtils.isBlank(pan)) {
return false;
}
// pan 必须是8-30位的数字
Matcher isPan = Pattern.compile("[0-9a-zA-Z]{8,30}").matcher(pan);
if (isPan.matches()) {
return true;
}
return false;
}
/**
* 校验短信策略名称
*
* @param sme
* @return
*/
public static boolean checkCustomer(String customer) {
if (StringUtils.isBlank(customer)) {
return false;
}
if (customer.length() < 4) {
return false;
}
if (Pattern.compile(letter_number_regexp).matcher(customer).matches()) {
return true;
}
return false;
}
// public static void main(String[] args) {
// checkByRegex("02023032", "\\d{8}[0-1]\\d{3}");
// }
/**
* 检查appCode是否是xbank
* 如果是xbank1.0或者xbank2.0,返回true;
* 否则返回false;
* @param appCode
* @return boolean
* */
public static boolean checkAppCodeIsXbank(String appCode) {
return "314".equals(appCode) || "301".equals(appCode);
}
/**
* 检查appCode是否是xbank1.0
* 如果是xbank1.0,返回true;
* 否则返回false;
* @param appCode
* @return boolean
* */
public static boolean checkAppCodeIsXbank1(String appCode) {
return "314".equals(appCode);
}
/**
* 检查appCode是否是xbank2.0
* 如果是xbank2.0,返回true;
* 否则返回false;
* @param appCode
* @return boolean
* */
public static boolean checkAppCodeIsXbank2(String appCode) {
return "301".equals(appCode);
}
/**
* 校验TokenNo,并截取令牌序列号校验位(12+1位校验位)
*
* @param tokenNo
* @return String 截取后的动态令牌号(12位),如果返回null,则令牌号校验失败
*/
public static String checkTokenID(String tokenNo) {
if (StringUtils.isBlank(tokenNo)) {
return null;
}
//如果令牌序列号为13位,去除最后一位的校验位
if(tokenNo.length() == 13){
tokenNo = tokenNo.substring(0,tokenNo.length()-1);
}
// TokenNo 必须是12位的数字
Matcher isTokenNo = Pattern.compile("\\d{12}").matcher(tokenNo);
if (isTokenNo.matches()) {
return tokenNo;
}
return null;
}
/**
* 校验OtpPass
*
* @param TokenNo
* @return
*/
public static boolean checkOtpPass(String otpPass) {
if (StringUtils.isBlank(otpPass)) {
return false;
}
else {
return true;
}
// OtpPass 必须是6位的数字
/*
* Matcher isOtpPass = Pattern.compile("\\d{6}").matcher(otpPass);
* if (isOtpPass.matches()) {
* return true;
* }
* return false;
*/
}
/**
* 校验随机数
*
* @param TokenNo
* @return
*/
public static boolean checkRandom(String random) {
if (StringUtils.isBlank(random)) {
return false;
}
// random 必须是6位的字符串
Matcher isOtpPass = Pattern.compile("\\w{6}").matcher(random);
if (isOtpPass.matches()) {
return true;
}
return false;
}
private static final Pattern PATTEN_EXPIREDDATE = Pattern.compile("^([1-2]\\d{3})(0\\d|1[0-2])([0-2]\\d|3[01])$");
public static boolean isValidDate(String date) {
Matcher matcher = PATTEN_EXPIREDDATE.matcher(date);
if (matcher.matches()) {
int year = Integer.valueOf(matcher.group(1));
int month = Integer.valueOf(matcher.group(2));
int day = Integer.valueOf(matcher.group(3));
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
if (calendar.get(Calendar.YEAR) == year && calendar.get(Calendar.MONTH) == month - 1
&& calendar.get(Calendar.DAY_OF_MONTH) == day) {
return true;
}
}
return false;
}
/**
* 校验pvk类型
* @param pvkType
* @return
*
*/
public static boolean checkPvk(String pvkType) {
Matcher isOtpPass = Pattern.compile("0[0-1]{1}[0-9]{1}[1-2]{1}").matcher(pvkType);
if (isOtpPass.matches()) {
return true;
}
else {
return false;
}
}
/**
* 判断是否是数字
* @param pvkType
* @return
*/
public static boolean checkDigital(String rsaPin) {
Matcher isOtpPass = Pattern.compile("[0-9]+").matcher(rsaPin);
if (isOtpPass.matches()) {
return true;
}
else {
return false;
}
}
/**
* 判断是否是数字+字母
* @param pvkType
* @return
*/
public static boolean checkDigitalAndLetter(String rsaPin) {
Matcher isOtpPass = Pattern.compile("[0-9a-zA-Z]+").matcher(rsaPin);
if (isOtpPass.matches()) {
return true;
}
else {
return false;
}
}
/**
* 判断是否是数字+字母+特殊字符
* @param pvkType
* @return
*/
public static boolean checkDigitalAndLetterAndSpecial(String rsaPin) {
Matcher isOtpPass = Pattern.compile("[0-9a-zA-Z@#$%^&*~]+").matcher(rsaPin);
if (isOtpPass.matches()) {
return true;
}
else {
return false;
}
}
public static boolean checkEmployeeId(String employeeId) {
if(employeeId.length() == 10){
Matcher matcher = Pattern.compile("[0-9]{10}").matcher(employeeId);
if(matcher.matches()){
return true;
}else{
matcher = Pattern.compile("[a-zA-Z]{1}[0-9]{9}").matcher(employeeId);
if(matcher.matches()){
return true;
}else{
return false;
}
}
}
return false;
}
public static boolean checkOAName(String oaName) {
Matcher matcher = Pattern.compile("[a-zA-Z0-9\\@\\-\\_\\.]{1,32}").matcher(oaName);
return matcher.matches();
}
public static boolean checkAuthCode(String authCode) {
if (StringUtils.isBlank(authCode)) {
return false;
}else{
Matcher matcher = Pattern.compile("(\\d{4})|(\\d{6})").matcher(authCode);
return matcher.matches();
}
}
}