给定
dateranges = [
"1***-**-**",
"-138 - ## - ##",
"18##-##-##",
"1713-##-##",
"####-##-##"
]
re
假设您不想正确执行此操作,您最好的解析器可能是:
import re
matcher = re.compile("(-?[\d*#]+)\s*-\s*([\d*#][\d*#])\s*-\s*([\d*#][\d*#])")
datetuples = [matcher.match(daterange).groups() for daterange in dateranges]
然后你可以通过元组,
for year, month, day in datetuples:
将每个未知数转换为数字和上限。
minyear = int(year.replace("*", "0").replace("#", "0"))
minmonth = max(1, int(month.replace("*", "0").replace("#", "0")))
minday = max(1, int(day.replace("*", "0").replace("#", "0")))
mindate = (minyear, minmonth, minday)
maxyear = int(year.replace("*", "9").replace("#", "9"))
maxmonth = min(12, int(month.replace("*", "9").replace("#", "9")))
### WARNING! MAXIMUM DAY NUMBER DEPENDS ON BOTH MONTH AND YEAR
maxday = min(31, int(day.replace("*", "9").replace("#", "9")))
maxdate = (maxyear, maxmonth, maxday)
print(mindate, maxdate)
#>>> (1000, 1, 1) (1999, 12, 31)
#>>> (-138, 1, 1) (-138, 12, 31)
#>>> (1800, 1, 1) (1899, 12, 31)
#>>> (1713, 1, 1) (1713, 12, 31)
#>>> (0, 1, 1) (9999, 12, 31)
请记住,由于上限,这会接受误报,并且还要记住大而大胆的警告。