📄 valid.java
字号:
/**
* Copyright: Copyright (c) 2006 by 徐建协
*/
package com.common.util;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
/**
* <p>File Name:Valid.java</p>
* <p>Title:</p>
* <p>Description:</p>
* <p>Company:</p>
* @author 徐建协 QQ:40255392 E-Mail:xujianxie@163.com
* @version 1.0 Sep 18, 2006
*/
public class Valid {
/** 整形表达式 */
public static final String intRegex = "^[+|-]?[\\d]+";
/** 浮点型表达式 */
public static final String floatRegex = "^[-\\+]?\\d+(\\.\\d+)?$";
/** 不大于两位小数点浮点型表达式 */
public static final String float2Regex = "^[-\\+]?\\d+(\\.\\d{1,2})?$";
/** 日期表达式 */
public static final String dateRegex = "^[\\d]{8}";
/** 邮编表达式 */
public static final String postRexgx = "^[\\d]{6}";
/**
* 整形检查
*
* @param src
* @return 正确返回true 错误返回false
*/
public static boolean isInt(String src) {
return isValid(src,Valid.intRegex);
}
/**
* 浮点型检查
*
* @param src
* @return 正确返回true 错误返回false
*/
public static boolean isFloat(String src) {
return isValid(src,Valid.floatRegex);
}
/**
* 浮点型检查
*
* @param src
* @return 正确返回true 错误返回false
*/
public static boolean isFloat2(String src) {
return isValid(src,Valid.float2Regex);
}
/**
* 日期检查
*
* @param src
* 检查的字符串
* @return 正确返回true 错误返回false
*/
public static boolean isDate(String src) {
if(isValid(src,Valid.dateRegex)){
return false;
}
int year = Integer.parseInt(src.substring(0, 4));
int month = Integer.parseInt(src.substring(4, 6));
int day = Integer.parseInt(src.substring(6, 8));
if (year < 1900)
return false;
if (month < 1 || month > 12)
return false;
if (StringUtil.getDaysOfMonth(year, month) < day)
return false;
return true;
}
/**
* 邮政编码检查
*
* @param src
* 检查的字符串
* @return 正确返回true 错误返回false
*/
public static boolean isPost(String src) {
return isValid(src,Valid.postRexgx);
}
/****************
*
* @param src 要检查的字符串
* @param regex 正则表达式
* @return 正确返回true 错误返回false
*/
public static boolean isValid(String src,String regex){
PatternCompiler compiler = new Perl5Compiler();
Pattern pattern=null;
try{
pattern= compiler.compile(regex);
}catch(Exception ex){
System.out.println(ex.toString());
}
PatternMatcher matcher = new Perl5Matcher();
return matcher.matches(src, pattern);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -