沒什麼好說的,提供正規表達式校驗。提供資料校驗。當解析excel,将cell的值填充到bean屬性中,提供支援。
validateutil.java
package com.ivfly.xlsbean;
import java.lang.reflect.invocationtargetexception;
import java.util.regex.matcher;
import java.util.regex.pattern;
import org.apache.commons.beanutils.methodutils;
public class validateutil {
/**
* email正規表達式=^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
*/
public static final string email = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
/**
* 電話号碼正規表達式= (^(\d{2,4}[-_-—]?)?\d{3,8}([-_-—]?\d{3,8})?([-_-—]?\d{1,7})?$)|(^0?1[35]\d{9}$)
public static final string phone = "(^(\\d{2,4}[-_-—]?)?\\d{3,8}([-_-—]?\\d{3,8})?([-_-—]?\\d{1,7})?$)|(^0?1[35]\\d{9}$)" ;
* 手機号碼正規表達式=^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$
public static final string mobile ="^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\\d{8}$";
* ip位址正規表達式 ((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))
public static final string ipaddress = "((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))";
* integer正規表達式 ^-?(([1-9]\d*$)|0)
public static final string integer = "^-?(([1-9]\\d*$)|0)";
* 正整數正規表達式 >=0 ^[1-9]\d*|0$
public static final string integer_negative = "^[1-9]\\d*|0$";
* 負整數正規表達式 <=0 ^-[1-9]\d*|0$
public static final string integer_positive = "^-[1-9]\\d*|0$";
* double正規表達式 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$
public static final string double ="^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$";
* 正double正規表達式 >=0 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
public static final string double_negative ="^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0$";
* 負double正規表達式 <= 0 ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
public static final string double_positive ="^(-([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*))|0?\\.0+|0$";
* 年齡正規表達式 ^(?:[1-9][0-9]?|1[01][0-9]|120)$ 比對0-120歲
public static final string age="^(?:[1-9][0-9]?|1[01][0-9]|120)$";
* 郵編正規表達式 [1-9]\d{5}(?!\d) 國内6位郵編
public static final string code="[1-9]\\d{5}(?!\\d)";
* 比對由數字、26個英文字母或者下劃線組成的字元串 ^\w+$
public static final string str_eng_num_="^\\w+$";
* 比對由數字和26個英文字母組成的字元串 ^[a-za-z0-9]+$
public static final string str_eng_num="^\\w+$";
* 比對由26個英文字母組成的字元串 ^[a-za-z]+$
public static final string str_eng="^[a-za-z]+$";
* 比對中文字元 ^[\u0391-\uffe5]+$
public static final string str_china="^[\u0391-\uffe5]+$";
* 過濾特殊字元串正則
* regex="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
public static final string str_special="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
*隻能輸英文 數字 中文 ^[a-za-z0-9\u4e00-\u9fa5]+$
public static final string str_eng_cha_num="^[a-za-z0-9\u4e00-\u9fa5]+$";
*
/***
* 日期正則 支援:
* yyyy-mm-dd
* yyyy/mm/dd
* yyyy_mm_dd
* yyyymmdd
* yyyy.mm.dd的形式
public static final string date_all="((^((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)$))";
public static final string date_format1="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
// 1.驗證日期的正規表達式加入閏年的判斷以及思路分析
//
// 進入正題之前,我們需要澄清兩個概念:
// 一,什麼是合法的日期範圍?對于不同的應用場景,這個問題有不同的解釋。這裡采納msdn中的約定:
// datetime 值類型表示值範圍在公元(基督紀元)0001 年 1 月 1 日午夜 12:00:00 到公元 (c.e.) 9999 年 12 月 31 日晚上 11:59:59 之間的日期和時間。
// 二,關于閏年的闡釋。人民教育出版社國小數學室的解釋淺明易懂(mediawiki等都沒說明為什麼整百年份必須是400的倍數時才是閏年):
// 關于公曆閏年是這樣規定的:地球繞太陽公轉一周叫做一回歸年,一回歸年長365日5時48分 46秒。是以,公曆規定有平年和閏年,平年一年有365日,比回歸年短0.2422日,四年共短0.9688日,故每四年增加一日,這一年有366日,就是閏年。但四年增加一日比四個回歸年又多0.0312日,400年後将多3.12日,故在400年中少設3個閏年,也就是在400年中隻設97個閏年,這樣公曆年的平均長度與回歸年就相近似了。由此規定:年份是整百數的必須是400的倍數才是閏年,例如1900年、2100年就不是閏年。
// 清楚了以上兩個概念,我們進入正題。
// 首先需要驗證年份,顯然,年份範圍為 0001 - 9999,比對yyyy的正規表達式為:
// [0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}
// 其中 [0-9] 也可以表示為 \d,但 \d 不如 [0-9] 直覺,是以下面我将一直采用 [0-9]
// 用正規表達式驗證日期的難點有二:一是大小月份的天數不同,二是閏年的考慮。
// 對于第一個難點,我們首先不考慮閏年,假設2月份都是28天,這樣,月份和日期可以分成三種情況:
// 1、月份為 1, 3, 5, 7, 8, 10, 12,天數範圍為 01 - 31,比對mm-dd的正規表達式為:
// (0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])
// 2、月份為 4, 6, 9, 11,天數範圍為 01-30,比對mm-dd的正規表達式為:
// (0[469]|11)-(0[1-9]|[12][0-9]|30)
// 3、月份為 2,考慮平年情況,比對mm-dd的正規表達式為:
// 02-(0[1-9]|[1][0-9]|2[0-8])
// 根據上面的成果,我們可以得到比對平年日期格式為yyyy-mm-dd的正規表達式:
// ([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))
// 接着我們來解決第二個難點:閏年的考慮。根據閏年的定義,我們可以将閏年分為兩類:
// 1、能被4整除但不能被100整除的年份。尋找後兩位的變化規律,可以很快得到下面的正則比對:
// ([0-9]{2})(0[48]|[2468][048]|[13579][26])
// 2、能被400整除的年份。能被400整除的數肯定能被100整除,是以後兩位肯定是00,我們隻要保證前兩位能被4整除即可,相應的正規表達式為:
// (0[48]|[2468][048]|[3579][26])00
// 2.最強驗證日期的正規表達式,添加了閏年的驗證
// 這個日期正規表達式支援
// yyyy-mm-dd
// yyyy/mm/dd
// yyyy_mm_dd
// yyyy.mm.dd的形式
// match : 2008-2-29 2008/02/29
// not match : 2008-2-30 2007-2-29
// 完整的正規表達式如下:
// ((^((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)$))
// 閏年的2月份有29天,是以比對閏年日期格式為yyyy-mm-dd的正規表達式為:
// (([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29
// 最後,将平年和閏年的日期驗證表達式合并,我們得到最終的驗證日期格式為yyyy-mm-dd的正規表達式為:
// (([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)
// dd/mm/yyyy格式的正則驗證表達式為:
// (((0[1-9]|[12][0-9]|3[01])/((0[13578]|1[02]))|((0[1-9]|[12][0-9]|30)/(0[469]|11))|(0[1-9]|[1][0-9]|2[0-8])/(02))/([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}))|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))
* url正規表達式
* 比對 http www ftp
public static final string url = "^(http|www|ftp|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?" +
"(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*" +
"(\\w*:)*(\\w*\\+)*(\\w*\\.)*" +
"(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$";
* 身份證正規表達式
public static final string idcard="((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})" +
"(([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}" +
"[xx0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3}))";
/**
* 1.比對科學計數 e或者e必須出現有且隻有一次 不含dd
* 正則 ^[-+]?(\d+(\.\d*)?|\.\d+)([ee]([-+]?([012]?\d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))$
*/
public final static string scientific_a ="^[-+]?(\\d+(\\.\\d*)?|\\.\\d+)([ee]([-+]?([012]?\\d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))$";
* 2.比對科學計數 e或者e必須出現有且隻有一次 結尾包含dd
* 正則 ^[-+]?(\d+(\.\d*)?|\.\d+)([ee]([-+]?([012]?\d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))[dd]?$
public final static string scientific_b ="^[-+]?(\\d+(\\.\\d*)?|\\.\\d+)([ee]([-+]?([012]?\\d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))[dd]?$";
* 3.比對科學計數 是否含有e或者e都通過 結尾含有dd的也通過(針對double類型)
* 正則 ^[-+]?(\d+(\.\d*)?|\.\d+)([ee]([-+]?([012]?\d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?[dd]?$
*/
public final static string scientific_c ="^[-+]?(\\d+(\\.\\d*)?|\\.\\d+)([ee]([-+]?([012]?\\d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?[dd]?$";
* 4.比對科學計數 是否含有e或者e都通過 結尾不含dd
* 正則 ^[-+]?(\d+(\.\d*)?|\.\d+)([ee]([-+]?([012]?\d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?$
public final static string scientific_d ="^[-+]?(\\d+(\\.\\d*)?|\\.\\d+)([ee]([-+]?([012]?\\d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?$";
////------------------驗證方法
* 判斷字段是否為空 符合傳回ture
* @param str
* @return boolean
public static synchronized boolean strisnull(string str) {
return null == str || str.trim().length() <= 0 ? true : false ;
}
* 判斷字段是非空 符合傳回ture
public static boolean strnotnull(string str) {
return !strisnull(str) ;
* 字元串null轉空
public static string nulltostr(string str) {
return strisnull(str)?"":str;
}
* 字元串null指派預設值
* @param str 目标字元串
* @param defaut 預設值
* @return string
public static string nulltostr(string str,string defaut) {
return strisnull(str)?defaut:str;
* 判斷字段是否為email 符合傳回ture
public static boolean isemail(string str) {
return regular(str,email);
* 判斷是否為電話号碼 符合傳回ture
public static boolean isphone(string str) {
return regular(str,phone);
* 判斷是否為手機号碼 符合傳回ture
public static boolean ismobile(string str) {
return regular(str,mobile);
* 判斷是否為url 符合傳回ture
public static boolean isurl(string str) {
return regular(str,url);
* 判斷是否為ip位址 符合傳回ture
public static boolean isipaddress(string str) {
return regular(str,ipaddress);
/**
* 判斷字段是否為數字 正負整數 正負浮點數 符合傳回ture
public static boolean isnumber(string str) {
return regular(str,double);
* 判斷字段是否為integer 符合傳回ture
public static boolean isinteger(string str) {
return regular(str,integer);
* 判斷字段是否為正整數正規表達式 >=0 符合傳回ture
public static boolean isinteger_negative(string str) {
return regular(str,integer_negative);
* 判斷字段是否為負整數正規表達式 <=0 符合傳回ture
public static boolean isinteger_positive(string str) {
return regular(str,integer_positive);
* 判斷字段是否為double 符合傳回ture
public static boolean isdouble(string str) {
* 判斷字段是否為正浮點數正規表達式 >=0 符合傳回ture
public static boolean isdouble_negative(string str) {
return regular(str,double_negative);
* 判斷字段是否為負浮點數正規表達式 <=0 符合傳回ture
public static boolean isdouble_positive(string str) {
return regular(str,double_positive);
* 判斷字段是否為日期 符合傳回ture
public static boolean isdate(string str) {
return regular(str,date_all);
/**
* 驗證2010-12-10
* @param str
* @return
*/
public static boolean isdate1(string str) {
return regular(str,date_format1);
* 判斷字段是否為年齡 符合傳回ture
public static boolean isage(string str) {
return regular(str,age) ;
* 判斷字段是否超長
* 字串為空傳回fasle, 超過長度{leng}傳回ture 反之傳回false
* @param leng
public static boolean islengout(string str,int leng) {
return strisnull(str)?false:str.trim().length() > leng ;
* 判斷字段是否為身份證 符合傳回ture
public static boolean isidcard(string str) {
if(strisnull(str)) return false ;
if(str.trim().length() == 15 || str.trim().length() == 18) {
return regular(str,idcard);
}else {
return false ;
}
* 判斷字段是否為郵編 符合傳回ture
public static boolean iscode(string str) {
return regular(str,code) ;
* 判斷字元串是不是全部是漢字
public static boolean ischina(string str) {
return regular(str,str_china) ;
* 判斷字元串是不是全部是英文字母
public static boolean isenglish(string str) {
return regular(str,str_eng) ;
* 判斷字元串是不是全部是英文字母+數字
public static boolean iseng_num(string str) {
return regular(str,str_eng_num) ;
* 判斷字元串是不是全部是英文字母+數字+下劃線
public static boolean iseng_num_(string str) {
return regular(str,str_eng_num_) ;
* 過濾特殊字元串 傳回過濾後的字元串
public static string filterstr(string str) {
pattern p = pattern.compile(str_special);
matcher m = p.matcher(str);
return m.replaceall("").trim();
* 比對是否符合正規表達式pattern 比對傳回true
* @param str 比對的字元串
* @param pattern 比對模式
private static boolean regular(string str,string pattern){
// system.out.println("pattern="+pattern);
if(null == str || str.trim().length()<=0)
return false;
pattern p = pattern.compile(pattern);
return m.matches();
* 判斷是不是科學計數法 如果是 傳回true
* 比對科學計數 e或者e必須出現有且隻有一次 結尾不含d
* 比對模式可參考本類定義的 scientific_a,scientific_b,scientific_c,scientific_d
* 若判斷為其他模式可調用 regular(string str,string pattern)方法
* @param str 科學計數字元串
public static boolean isscientific(string str){
if(strisnull(str))
return false;
return regular(str,validateutil.scientific_a);
}
public static void main(string args[]){
string value = "3232";
boolean b = validateutil.isemail(value);
system.out.println("b="+b);
try {
object r = methodutils.invokestaticmethod(validateutil.class,"isemail",value);
system.out.println("r="+r);
} catch (nosuchmethodexception e) {
e.printstacktrace();
} catch (illegalaccessexception e) {
} catch (invocationtargetexception e) {
}
//測試動态方法調用
}
}