zoukankan      html  css  js  c++  java
  • Java正则验证

    手写正则匹配工具类,以便于以后使用方便,后续有空了不断优化补充....................

    常量类

    public class RegexConstant {
        //匹配Email
        public static final int E_MAIL=0;
        //匹配手机
        public static final int MOBIL_PHONE=1;
        //匹配电话座机
        public static final int TEL_PHONE=2;
        //匹配邮编
        public static final int ZIP_CODE=3;
        //匹配URL
        public static final int URL=4;
        //匹配ip格式
        public static final int IP=5;
        //匹配HTML标签
        public static final int HTML_TAG=6;
        //匹配十六进制
        public static final int HEX=7;
        //匹配QQ号
        public static final int QQ=8;
        //匹配身份证号
        public static final int ID_CARD=9;
        //匹配正整数
        public static final int POSITIVE_INTEGER=10;
    }

     工具类

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class RegexUtil {
        //正则表达式的字符串
        private String regexStr=new String();
    
        //正则校验,如果创建对象但是不传入正则表达式而调用该方法就会报错
        public  boolean customMatcher(String testStr){//传入待检测的字符串
            Pattern pattern=Pattern.compile(regexStr);
            Matcher matcher = pattern.matcher(testStr);
            return matcher.matches();
        }
        //正则表达式通用匹配
        private static boolean genericMatcher(String regexExpre,String testStr){
            Pattern pattern=Pattern.compile(regexExpre);
            Matcher matcher = pattern.matcher(testStr);
            return matcher.matches();
        }
    
        //匹配器
        public static boolean matcher(int RegexConstant_NAME,String testStr){
            boolean flag=false;
            switch (RegexConstant_NAME){
                case RegexConstant.E_MAIL:flag=genericMatcher("\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",testStr);break;
                case RegexConstant.HEX:flag=genericMatcher("/^#?([a-f0-9]{6}|[a-f0-9]{3})$/",testStr);break;
                case RegexConstant.IP:flag=genericMatcher("/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/",testStr);break;
                case RegexConstant.HTML_TAG:flag=genericMatcher("/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/",testStr);break;
                case RegexConstant.ZIP_CODE:flag=genericMatcher("/^[u4e00-u9fa5],{0,}$/",testStr);break;
                case RegexConstant.QQ:flag=genericMatcher("[1-9][0-9]{4,}",testStr);break;
            }
            return flag;
        }
        public RegexUtil(String regexStr) {
            this.regexStr = regexStr;
        }
    
        public String getRegexStr() {
            return regexStr;
        }
    
        public void setRegexStr(String regexStr) {
            this.regexStr = regexStr;
        }
    }

     目前API说明

    需求1:使用自定义的正则表达式并校验我的待匹配字符串是否符合要求

    customMatcher(String testStr) 自定义正则匹配的方法

    如果使用自定义正则匹配表达式进行字符串匹配,需要通过有参构造先传入正则表达式创建RegexUtil的对象,通过对象调用customMatcher(String testStr)传入待匹配的字符串进行匹配,为预防空指针异常,本类没有提供无参构造

    System.out.println(new RegexUtil("[0-9]{3}").customMatcher("213"));

     结果为true

    需求2:使用工具类提供的表达式进行匹配字符串

    genericMatcher(String regexExpre,String testStr)通用正则匹配器

    只需要通过RegexUtil.genericMatcher(String regexExpre,String testStr)即可实现字符串匹配,第一个参数是预先定义的常量,第二个表达式是待匹配的字符串,如

    boolean reslut = RegexUtil.matcher(RegexConstant.E_MAIL, "1433123@163.com");
    System.out.println(reslut);

     结果为true

     后续需求待完善中....

  • 相关阅读:
    U132973 双生独白
    Remmarguts' Date(A* 短路)
    P3908 数列之异或
    P1469 找筷子
    P1759 通天之潜水
    P2356 弹珠游戏
    P7072 直播获奖
    P7074 方格取数
    CSP2020二轮游记
    P6205 [USACO06JAN]Dollar Dayz S
  • 原文地址:https://www.cnblogs.com/kitor/p/11322301.html
Copyright © 2011-2022 走看看