• Java正则表达式收藏


    开贴在此记录正则表达式,不定期更新~~~

    一、匹配时间

    ①匹配[yyyy-MM-dd]的正则表达式:

    String reg_yyyy_MM_dd = "(([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)";

    ②匹配[yyyyMMdd]的正则表达式:

    String reg_yyyyMMdd = "(([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))0229)";

    ③匹配[dd/MM/yyyy]的正则表达式:

    String reg_dd_MM_yyyy = "(((0[1-9]|[12][0-9]|3[01])/((0[13578]|1[02]))|((0[1-9]|[12][0-9]|30)/(0[469]|11))|(0[1-9]|[1][0-9]|2[0-8])/(02))" +
                    "/([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}))|" +
                    "(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))";

    网上传的正则表达式比较多,但是有些确实不靠谱,怎么证明我上面的几个靠谱呢?测试

    附上测试代码:

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
    public class DateCheck {
        public static void main(String[] args) {
            //yyyy-MM-dd 
            String reg_yyyy_MM_dd = "(([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)";
            
            //yyyyMMdd 
            String reg_yyyyMMdd = "(([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))0229)";
            
            //dd/MM/yyyy
            String reg_dd_MM_yyyy = "(((0[1-9]|[12][0-9]|3[01])/((0[13578]|1[02]))|((0[1-9]|[12][0-9]|30)/(0[469]|11))|(0[1-9]|[1][0-9]|2[0-8])/(02))" +
                    "/([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}))|" +
                    "(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))";
            
            
            Calendar calendar = Calendar.getInstance();
            calendar.set(1970, 0, 1, 0, 0, 0);
            
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat format3 = new SimpleDateFormat("dd/MM/yyyy");
            
            Calendar today = Calendar.getInstance();
            while(calendar.before(today)){
                String dayString1 = format1.format(calendar.getTime());
                String dayString2 = format2.format(calendar.getTime());
                String dayString3 = format3.format(calendar.getTime());
                
                if(!dayString1.matches(reg_yyyy_MM_dd)){
                    System.out.println(dayString1+"matches [yyyy-MM-dd] fail!");
                }
                if(!dayString2.matches(reg_yyyyMMdd)){
                    System.out.println(dayString2+"matches [yyyyMMdd] fail!");
                }
                if(!dayString3.matches(reg_dd_MM_yyyy)){
                    System.out.println(dayString2+"matches [dd/MM/yyyy] fail!");
                }
                calendar.add(Calendar.DAY_OF_MONTH, 1);//增加一天
            }
            System.out.println("检测完毕!");
        }
    }

    这些个正则表达式写起来有点小复杂,不过发现有个哥们讲述的挺详细的,感兴趣的话可以猛击这里

  • 相关阅读:
    jmeter接口测试----10从文件中读取
    How to let a html with JavaScript can display in Outlook?
    [Jenkins] 将上游Job的参数传递给多个下游Job
    Jenkins Job之间传递参数
    一个Jenkins job里面配置多个批处理脚本,如果其中某一个有fail的,其后面的批处理脚本全都不执行了
    JMeter supports dashboard report generation to get graphs and statistics from a test plan
    分享几个实用的批处理脚本
    Job构建步骤间的自定义变量传递
    Download file from a URL using AutoIt, and run in Robot Framework. (Also can use in other application)
    [Selenium] Upload File on Remote Node Machines
  • 原文地址:https://www.cnblogs.com/yejg1212/p/3103960.html
Copyright © 2020-2023  润新知