这是一种使正则表达式与所需格式的任何日期匹配的方法(尽管您显然可以调整逗号是否可选,添加月份缩写等等):
years = r'((?:19|20)dd)'pattern = r'(%%s) +(%%s), *%s' % yearsthirties = pattern % ( "September|April|June|November", r'0?[1-9]|[12]d|30')thirtyones = pattern % ( "January|March|May|July|August|October|December", r'0?[1-9]|[12]d|3[01]')fours = '(?:%s)' % '|'.join('%02d' % x for x in range(4, 100, 4))feb = r'(February) +(?:%s|%s)' % ( r'(?:(0?[1-9]|1d|2[0-8])), *%s' % years, # 1-28 any year r'(?:(29), *((?:(?:19|20)%s)|2000))' % fours) # 29 leap years onlyresult = '|'.join('(?:%s)' % x for x in (thirties, thirtyones, feb))r = re.compile(result)print result
然后我们有:
>>> r.match('January 30, 2001') is not NoneTrue>>> r.match('January 31, 2001') is not NoneTrue>>> r.match('January 32, 2001') is not NoneFalse>>> r.match('February 32, 2001') is not NoneFalse>>> r.match('February 29, 2001') is not NoneFalse>>> r.match('February 28, 2001') is not NoneTrue>>> r.match('February 29, 2000') is not NoneTrue>>> r.match('April 30, 1908') is not NoneTrue>>> r.match('April 31, 1908') is not NoneFalse
您可能会问,这个光荣的正则表达式是什么?
>>> print result(?:(September|April|June|November) +(0?[1-9]|[12]d|30), *((?:19|20)dd))|(?:(January|March|May|July|August|October|December) +(0?[1-9]|[12]d|3[01]), *((?:19|20)dd))|(?:February +(?:(?:(0?[1-9]|1d|2[0-8]), *((?:19|20)dd))|(?:(29), *((?:(?:19|20)(?:04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96))|2000))))
(我最初打算对可能的日期进行逐个枚举,但基本上我还是用手写的方式写了整个粗略的东西,但无论如何都是四个的倍数。)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)