• Java使用正则校验各种信息


    一、前言
    在实际项目中,我们有可能需要针对身份证、IP地址,手机号等相关信息做校验,这是可以通过正则匹配形式实现相关校验。

    二、具体实现
    1.先定义相关信息校验工具类。

    public class RegexUtil {
        public static final String identity_regex="^[1-9][0-9]{5}(1[8-9][0-9]{2}|[2-9][0-9]{3})((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3}[0-9Xx]$";
    
        public static final String ip_regex="^((2(5[0-5]|[0-4][0-9]))|[0-1]?[0-9]{1,2})(\\.((2(5[0-5]|[0-4][0-9]))|[0-1]?[0-9]{1,2})){3}$";
    
        public static final String phone_regex="^0[0-9]{2,3}-[0-9]{7,8}|(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])[0-9]{8}$";
    
        public static final String email_regex="^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$";
        public static final String date_regex="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
        /**
         * 校验身份证号
         * @param identityCard
         * @return
         */
        public static boolean checkIdentityCard(String identityCard){
            Pattern pattern=Pattern.compile(identity_regex);
            Matcher matcher=pattern.matcher(identityCard);
            return matcher.matches();
        }
    
        /**
         * 校验IP地址
         * @param ip
         * @return
         */
        public static boolean checkIpAddress(String ip){
            Pattern pattern=Pattern.compile(ip_regex);
            Matcher matcher=pattern.matcher(ip);
            return matcher.matches();
        }
    
        /**
         * 校验手机号
         * @param phone
         * @return
         */
        public static boolean checkPhone(String phone){
            Pattern pattern=Pattern.compile(phone_regex);
            Matcher matcher=pattern.matcher(phone);
            return matcher.matches();
        }
    
         /**
         * 校验邮箱
          * @param email
         * @return
         */
        public static boolean checkEmail(String email){
            Pattern pattern=Pattern.compile(email_regex);
            Matcher matcher=pattern.matcher(email);
            return matcher.matches();
        }
    
        /**
         * 校验日期(格式为 yyyy-MM-dd)
         * @param date
         * @return
         */
        public static boolean checkDate(String date){
            Pattern pattern=Pattern.compile(date_regex);
            Matcher matcher=pattern.matcher(date);
            return matcher.matches();
        }
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    2.接着可以测试来验证下。

    public class test{
     	public static void main(String[] args) {
            System.err.println(RegexUtil.checkIdentityCard("362421199408320912"));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    结果如下:
    在这里插入图片描述
    接着将身份证号的32改为31

     public static void main(String[] args) {
            System.err.println(checkIdentityCard("362421199408310912"));
        }
    
    • 1
    • 2
    • 3

    结果如下:
    在这里插入图片描述

  • 相关阅读:
    python——第九天
    Illegal key size or default parameters
    ESP8266-Arduino编程实例-LM75温度传感器驱动
    Java进阶篇
    删除有序数组中的重复项Ⅱ--------题解报告
    D. Problem with Random Tests(暴力&贪心)
    Redis面试问题三什么是缓存雪崩怎么解决
    集合原理简记
    JSP第一篇 -----JSP浅聊EL表达式第一篇: 基础操作,以及域搜索顺序以及EL隐式对象
    牛客网趋势最热Java八股文,已帮助上千人拿到大厂offer
  • 原文地址:https://blog.csdn.net/qq_42077317/article/details/132804062