工作便利贴---Python年月日时分秒正则匹配
Python年月日时分秒正则匹配
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# DevVersion: Python3.6.8
# Date: 2020-08-14 14:44
# Author: SunXiuWen
# PyCharm|test
import re
"""
格式:yyyymmddHHMMSS
年:\d{4} yyyyy
月:0[1-9]|1[0-2] mm
日:0[1-9]|[1-2][0-9]|3[0-1] dd
时:0[0-9]|1[0-9]|2[0-3] HH
分:[0-5][0-9] MM
秒:[0-5][0-9] SS
"""
test = "20201231235959"
# ret = re.match(r'\d{4}[0][1-9]\d{4}[0-5][0-9][0-5][0-9]|\d{4}[1][0-2]\d{4}[0-5][0-9][0-5][0-9]', test)
# ret = re.match(r'(\d{4}[0][1-9]|\d{4}[1][0-2])\d{4}[0-5][0-9][0-5][0-9]', test)
# ret = re.match(r'(\d{4}[0][1-9]|\d{4}[1][0-2])\d{4}([0-5][0-9])\2', test)
# ret = re.match(r'(\d{4}0[1-9]|\d{4}1[0-2])\d{4}([0-5][0-9])\2', test)
# ret = re.match(r'(\d{4}0[1-9]|\d{4}1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])\d{2}([0-5][0-9])\3', test)
ret = re.match(r'(\d{4}0[1-9]|\d{4}1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])(0[0-9]|1[0-9]|2[0-3])([0-5][0-9])\4', test)
if ret:
print("True")
else:
print("False")
print(ret)
人生苦短,我用python!