功能:給定開始日期和結(jié)束日期,返回匹配該段日期區(qū)間的正則表達(dá)式。生成的正則表達(dá)式假定了每個(gè)月都有39天例证,即01-39,所以不要使用該模塊來判斷日期的合法性迷捧。該模塊生成的正則表達(dá)式的使用場(chǎng)景是從一組合法日期中提取給定區(qū)間內(nèi)的日期织咧。源碼如下,使用scala實(shí)現(xiàn)漠秋。
def genRegexp(start:String,end:String): String = {
//s:開始e:結(jié)束y:年m:月d:日o:個(gè)位t:十位
//sy:開始年em:結(jié)束月笙蒙,其他變量命名規(guī)則相同
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 {
//構(gòu)造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)
//構(gòu)造PB,已知問題,結(jié)束年不可為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)"
}
//構(gòu)造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
}