java正则表达式验证整数、浮点数和日期

/***检查日期格式*@paramdate*@return*/publicstaticbooleancheckDate(Stringdate){StringeL="^((\\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-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";Patternp=Pattern.compile(eL);Matcherm=p.matcher(date);booleanb=m.matches();returnb;}/***检查整数*@paramnum*@paramtype"0+":非负整数"+":正整数"-0":非正整数"-":负整数"":整数*@return*/publicstaticbooleancheckNumber(Stringnum,Stringtype){StringeL="";if(type.equals("0+"))eL="^\\d+$";//非负整数elseif(type.equals("+"))eL="^\\d*[1-9]\\d*$";//正整数elseif(type.equals("-0"))eL="^((-\\d+)|(0+))$";//非正整数elseif(type.equals("-"))eL="^-\\d*[1-9]\\d*$";//负整数elseeL="^-?\\d+$";//整数Patternp=Pattern.compile(eL);Matcherm=p.matcher(num);booleanb=m.matches();returnb;}/***检查浮点数*@paramnum*@paramtype"0+":非负浮点数"+":正浮点数"-0":非正浮点数"-":负浮点数"":浮点数*@return*/publicstaticbooleancheckFloat(Stringnum,Stringtype){StringeL="";if(type.equals("0+"))eL="^\\d+(\\.\\d+)?$";//非负浮点数elseif(type.equals("+"))eL="^((\\d+\\.\\d*[1-9]\\d*)|(\\d*[1-9]\\d*\\.\\d+)|(\\d*[1-9]\\d*))$";//正浮点数elseif(type.equals("-0"))eL="^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";//非正浮点数elseif(type.equals("-"))eL="^(-((\\d+\\.\\d*[1-9]\\d*)|(\\d*[1-9]\\d*\\.\\d+)|(\\d*[1-9]\\d*)))$";//负浮点数elseeL="^(-?\\d+)(\\.\\d+)?$";//浮点数Patternp=Pattern.compile(eL);Matcherm=p.matcher(num);booleanb=m.matches();returnb;}

相关推荐