Java正则验证

本文介绍了一个自定义的手写正则表达式工具类,包括常用的正则表达式匹配,如Email、手机号、URL等,并提供了自定义正则表达式匹配方法,适用于多种字符串验证场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

常量类

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

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

转载于:https://www.cnblogs.com/kitor/p/11322301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值