码迷,mamicode.com
首页 > 其他好文 > 详细

日期区间正则表达式生成模块

时间:2017-07-03 16:29:51      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:start   log   form   变量   pcl   logs   exp   判断   nbsp   

功能:给定开始日期和结束日期,返回匹配该段日期区间的正则表达式。生成的正则表达式假定了每个月都有39天,即01-39,所以不要使用该模块来判断日期的合法性。该模块生成的正则表达式的使用场景是从一组合法日期中提取给定区间内的日期。源码如下,使用scala实现。

def genRegexp(start:String,end:String): String = {
        //s:开始e:结束y:年m:月d:日o:个位t:十位
        //sy:开始年em:结束月,其他变量命名规则相同
        val sy = start.substring(0, 4)
        val sm = start.substring(4, 6)
        val sd = start.substring(6, 8)
        val ey = end.substring(0, 4)
        val em = end.substring(4, 6)
        val ed = end.substring(6, 8)
        val sdo = sd.substring(1, 2)
        val sdt = sd.substring(0, 1)
        val smo = sm.substring(1, 2)
        val smt = sm.substring(0, 1)
        val syo = sy.substring(3, 4)
        val syt = sy.substring(2, 3)
        val edo = ed.substring(1, 2)
        val edt = ed.substring(0, 1)
        val emo = em.substring(1, 2)
        val emt = em.substring(0, 1)
        val eyo = ey.substring(3, 4)
        val eyt = ey.substring(2, 3)
        var PAL = ""
        var PAR = ""
        var PA = ""
        var PB = ""
        var PC = ""
        var PCL = ""
        var PCR = ""
        var pattern = ""
        var PL = ""
        var PR = ""
        var PRT = ""
        if (sy == ey) {
            if (sm == em) {
                if (sdt == edt) {
                    pattern = "(^%s%s%s[%s-%s]$)".format(sy, sm, sdt, sdo, edo)
                }
                else {
                    if (sdt.toInt + 1 == edt.toInt) {
                        pattern = "(^%s%s((%s[%s-9])|(%s[0-%s]))$)".format(sy, sm, sdt, sdo, edt, edo)
                    }
                    else {
                        pattern = "(^%s%s((%s[%s-9])|(%s[0-%s])|([%d-%d]\\d))$)".format(sy, sm, sdt, sdo,
                            edt, edo, sdt.toInt + 1, edt.toInt - 1)
                    }
                }
            }
            else {
                if (sdt == "3") {
                    PL = "(%s3[%s-9])".format(sm, sdo)
                }
                else {
                    PL = "(%s((%s[%s-9])|([%d-3]\\d)))".format(sm, sdt, sdo, sdt.toInt + 1)
                }
                if (edt == "0") {
                    PR = "(%s0[0-%s])".format(em, edo)
                }
                else {
                    PR = "(%s(([0-%d]\\d)|(%s[0-%s])))".format(em, edt.toInt - 1, edt, edo)
                }
                PRT = "(pass)"
                if (sm.toInt + 1 != em.toInt) {
                    val sma = sm.toInt + 1
                    val emr = em.toInt - 1
                    if (sma < 10) {
                        if (emr < 10) {
                            PRT = "(0[%d-%d]\\d{2})".format(sma, emr)
                        }
                        else {
                            PRT = "(((0[%d-9])|(1[0-%d]))\\d{2})".format(smo.toInt + 1, emo.toInt - 1)
                        }
                    }
                    else {
                        PRT = "((1[%s-%s])\\d{2})".format((sma.toString).substring(1,2),
                            (emr.toString).substring(1,2))
                    }
                }
                pattern = "(^%s(%s|%s|%s)$)".format(sy, PL, PR, PRT)
            }
        }
        else {
            //构造PA
            if (sdt == "3") {
                PAL = "%s(%s[%s-9])".format(sm, sdt, sdo)
            }
            else {
                PAL = "(%s((%s[%s-9])|([%d-3]\\d)))".format(sm, sdt, sdo, sdt.toInt + 1)
            }
            if (sm.toInt == 12) {
                PAR = "(pass)"
            }
            else if (sm.toInt >= 9) {
                var sm2 = (sm.toInt + 1).toString
                var smo2 = sm2.substring(1,2)
                PAR = "(1[%s-2]\\d\\d)".format(smo2)
            }
            else {
                PAR = "(((0[%d-9])|(1[0-2]))\\d{2})".format(smo.toInt + 1)
            }
            PA = "(^%s(%s|%s)$)".format(sy, PAL, PAR)
            //构造PB,已知问题,结束年不可为2000
            if (sy.toInt + 1 != ey.toInt) {
                var sy2 = (sy.toInt + 1).toString
                var syt2 = sy2.substring(2,3)
                var syo2 = sy2.substring(3,4)
                var ey2 = (ey.toInt - 1).toString
                var eyt2 = ey2.substring(2,3)
                var eyo2 = ey2.substring(3,4)
                if (syt2 == eyt2) {
                    PB = "(^20%s[%s-%s]\\d{4}$)".format(syt2, syo2, eyo2)
                }
                else {
                    if (syt2.toInt + 1 != eyt2.toInt) {
                        PB = "(^20((%s[%d-9])|([%d-%d]\\d)|(%s[0-%s]))\\d{4}$)".format(syt2, syo2.toInt,
                            syt2.toInt + 1, eyt2.toInt - 1, eyt2, eyo2)
                    }
                    else {
                        PB = "(^20((%s[%d-9])|(%s[0-%s]))\\d{4}$)".format(syt2, syo2.toInt,
                            eyt2, eyo2)
                    }
                }
            }
            else {
                PB = "(pass)"
            }
            //构造PC
            if (edt == "0") {
                PCR = "(%s%s[0-%s])".format(em, edt, edo)
            }
            else {
                PCR = "(%s(([0-%d]\\d)|(%s[0-%s])))".format(em, edt.toInt - 1, edt, edo)
            }
            if (em.toInt >= 11) {
                PCL = "(((0[1-9])|(1[0-%d]))\\d{2})".format( emo.toInt - 1)
            }
            else if (em.toInt == 10) {
                PCL = "(0[1-9]\\d{2})"
            }
            else {
                PCL = "(0[0-%d]\\d{2})".format(emo.toInt - 1)
            }
            PC = "(^%s(%s|%s)$)".format(ey, PCL, PCR)
            pattern = "%s|%s|%s".format(PA, PB, PC)
        }
        pattern
    }

 

日期区间正则表达式生成模块

标签:start   log   form   变量   pcl   logs   exp   判断   nbsp   

原文地址:http://www.cnblogs.com/cutebxy/p/7111312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!