莫大人

正则表达式校验数据

package com.midea.jr.efc.eac.commons;

import java.util.regex.Pattern;

import com.midea.jr.commons.utils.AssertUtil;


public class Validator {
    
    private static final String NUMWORD = "^[^\u4e00-\u9fa5]{0,}$"; //非汉字
    private static final String NUMWORDMINUS = "^[\\-]*[A-Za-z0-9]+$"; //数字和字母
    private static final String CHINESE = "^^[\u4E00-\u9FA5\uF900-\uFA2D]+$"; //中文
    private static final String NUMBER = "^[0-9]+$"; //数字
    private static final String WORD = "^[\u4e00-\u9fa5_a-zA-Z0-9]+$"; //中文,字母,数字
    
    public static boolean check(String reg,String value){
        return Pattern.matches(reg,value); 
    }
    
    public static boolean checkNumWord(String v){
        return check(NUMWORD,v); 
    }
    
    public static boolean checkNumWordMinus(String v){
        return check(NUMWORDMINUS,v); 
    }
    /**
     * 是否数字
     * @param v
     * @return
     */
    public static boolean checkNumber(String v){
        return check(NUMBER,v); 
    }
    /**
     * 是否由中文,字母,数字组成
     * @param v
     * @return
     */
    public static boolean checkWord(String v){
        return check(WORD,v); 
    }
    
    public static boolean checkChinese(String v){
        return check(CHINESE,v); 
    }

    public static boolean isNull(String v){
        if(v == null||v.equals("")){
            return true;
        }
        return false; 
    }

    public static void main(String[] args) {
//        System.out.println(checkChinese("哈哈a3哈"));
//        System.out.println(checkNumWord("33所属aa"));
//        System.out.println(Pattern.matches(NUMBER,"23"));
//        System.out.println(Pattern.matches(WORD,"23a号"));
//        AssertUtil.isTrue(Validator.checkNumber("a"),ErrorCodeEnum.TYPE_VALID.getErrordesc());
//        AssertUtil.isTrue(Validator.checkWord("a"),ErrorCodeEnum.TYPE_VALID.getErrordesc());
        
//        System.out.println(Validator.checkNumWord("aaa_"));
        System.out.println(Validator.check("(^([\u4E00-\u9FA5\uF900-\uFA2D]){1,20}$)||(^([A-Za-z]){2,40}$)","哈哈成擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦哈"));
        
    }
}

 

posted on 2017-03-14 13:37  莫大人  阅读(351)  评论(0编辑  收藏  举报

导航