微软 linux命令 linux google 程序员 apache Python Firefox Ubuntu HTML5 centos java php Android shell Windows wordpress 开源 mysql nginx

封装的Java正则匹配类,模拟PHP实现

今天修改了之前写的代码,其中有一个JAVA的正则类,模拟了PHP的几个函数,熟悉php的一看就清楚用法。支持常见的正则匹配,用法简单易懂。有需要的可以拿去,免得到处找来找去的麻烦。
    正则枚举类:

public enum FILTER {

            EMAIL("(?i)^[_a-z0-9\-]+([._a-z0-9\-]+)*@[a-z0-9\-]+([.a-z0-9\-]+)*(\.[a-z]{2,4})$"),

            NUMBER("[+-]?\d+[.0-9]*"),

            MOBILEPHONE("^(13|15|18)\d{9}$"), 

            MOBILE_CM("^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\d)\d{7}$"), 

            MOBILE_CU("^1(3[0-2]|5[256]|8[56])\d{8}$"), 

            MOBILE_CT("^1((33|53|8[09])[0-9]|349)\d{7}$"), 

            PHONES("^0(10|2[0-9]|\d{3})\d{7,8}$"), 

            URL("(?i)^(http|https|www|ftp|)?(://)?([_a-z0-9\-].)+[_a-z0-9\-]+(\/[_a-z0-9\-])*(\/)*([_a-z0-9\-].)*([_a-z0-9\-&#?=])*$"), 

            CASE("^[a-zA-Z]+$"), 

            IPV4("(?<=(\b|\D))(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))(?=(\b|\D))"),

            IPV6(".*/([0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+(%[a-zA-Z0-9]+)?):([0-9]+)"), 

            CHINESE("[\u4e00-\u9fa5]*"), 

            TWOBYTES("[^\x00-\xff]"), 

            IDCARD("IDCARD"), 

            CJK("\p{InCJK Unified Ideographs}"),

            ISBN10("^(?:(\d{9}[0-9X])|(?:(\d{1,5})(?:\-|\s)(\d{1,7})(?:\-|\s)(\d{1,6})(?:\-|\s)([0-9X])))$"), 

            ISBN13("^(978|979)(?:(\d{10})|(?:(?:\-|\s)(\d{1,5})(?:\-|\s)(\d{1,7})(?:\-|\s)(\d{1,6})(?:\-|\s)([0-9])))$"), 

            ISEMPTY("ISEMPTY");

    private String regexp;

    private FILTER(String regexp) {

        this.regexp = regexp;

    }

    public String getRegexp() {

        return regexp;

    }  

    public static boolean getIDCARD(String idcard){

        return IdcardValidator.isValidatedAllIdcard(idcard);

    }    

    public static boolean getISEMPTY(String str){

        return (str == null) || (str.trim().length() < 1);

    }    

}

里面列举了常见的正则,对于无法或不适用正则判断的情况,就调用其它类里方法进行判断.实现类如下:

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

 * 常用验证助手

 * @author 白的菜

 * @date @verson

 */

public final class ValidatorHelper {

    

    public static Boolean filter_var(final String valids,final FILTER type,String... addon){

        boolean retval = false;

        switch(type.name()){

        case "IDCARD":

            retval=FILTER.getIDCARD(valids);

            break;

        case "ISEMPTY":

            retval=FILTER.getISEMPTY(valids);

            break;

        default:

            retval = valids.matches(type.getRegexp());

        }        

        return retval;

    }

    public static Boolean filter_var_free(final String valids,final String reg){

        boolean retval = false;

        retval = valids.matches(reg);

        return retval;

    }

    public static List<String> preg_match_all(final String source, final String regex) {

        List<String> result = new ArrayList<String>();

        final Pattern p = Pattern.compile(regex);

        Matcher matcher = p.matcher(source);

        while (matcher.find()) {

                result.add(matcher.group());    

        }

        return result;

    }

    public static String preg_match(final String source, final String regex) {

        Pattern p = Pattern.compile(regex);

        Matcher matcher = p.matcher(source);

        return matcher.find() ? matcher.group() : "";

    }

    public static String preg_replace(final String source, final String regex, final String replacement) {

        Pattern p = Pattern.compile(regex);

        Matcher matcher = p.matcher(source);

        return matcher.find() ? source.replace(matcher.group(), replacement) : source;

    }

    

    /**

     * 判断type是否基本类型,如String,Boolean,Number,Date

     */

    public static Boolean basicType(Class type) {

        if (type.isArray()) {// 如是数组

            return basicType(type.getComponentType());// 判断元素类型

        } else {

            return type.isPrimitive() || type.equals(String.class) || type.equals(Boolean.class) || Number.class.isAssignableFrom(type) || java.util.Date.class.isAssignableFrom(type);

        }

    }

}

测试如下:

        out.println(ValidatorHelper.filter_var("ppmm@qq.com", FILTER.EMAIL));

        out.println(ValidatorHelper.filter_var("202.255.15.238", FILTER.IPV4));

        out.println(ValidatorHelper.filter_var("978-7-115-29031-6",

                FILTER.ISBN13));

        out.println(ValidatorHelper.filter_var("360722198808290528",

                FILTER.IDCARD));

        out.println(ValidatorHelper.filter_var("360722198808290527",

                FILTER.IDCARD));

        String emailS = "admin@qq.com,ma_hua_teng2012@163.com,mayun@alibaba.com.cn";

    String emailReg = "(?i)[_a-z0-9\-]+([._a-z0-9\-]+)*@[a-z0-9\-]+([.a-z0-9\-]+)*(\.[a-z]{2,4})";

        List<String> list = new ArrayList<String>();

        list = ValidatorHelper.preg_match_all(emailS, emailReg);

        for (String string : list) {

            out.println(string);

        }

延伸阅读

评论