# !/usr/bin/python3
# coding: utf-8
import re
import traceback
def find(pattern, string):
result = re.findall(pattern, string)
if result:
return str(result[0])
else:
return ''
def ipv4():
return r'(?:\d{1,3}\.){3}\d{1,3}'
def date_time24(ymd_symbol='-', ymd_hms_symbol=' ', hms_symbol=':'):
pattern_ymd = date(ymd_symbol)
pattern_hms24 = time24(hms_symbol)
return r'(?:%s)%s(?:%s)' % (pattern_ymd, ymd_hms_symbol, pattern_hms24)
def date_time12(ymd_symbol='-', ymd_hms_symbol=' ', hms_symbol=':'):
pattern_ymd = date(ymd_symbol)
pattern_hms12 = time12(hms_symbol)
return r'(?:%s)%s(?:%s)' % (pattern_ymd, ymd_hms_symbol, pattern_hms12)
def date(ymd_symbol='-'):
# 一三五七八十腊,三十一天永不差
pattern_ymd1 = r'(?:\d{4})%s(?:0[13578]|1[02])%s(?:0[1-9]|[12]\d|3[01])' % (ymd_symbol, ymd_symbol)
# 四六九十一
pattern_ymd2 = r'(?:\d{4})%s(?:0[469]|11)%s(?:0[1-9]|[12]\d|30)' % (ymd_symbol, ymd_symbol)
# 二月一到廿八
pattern_ymd3 = r'(?:\d{4})%s(?:02)%s(?:0[1-9]|1\d|2[0-8])' % (ymd_symbol, ymd_symbol)
# 四年一闰,百年不闰
pattern_ymd4 = r'(?:\d{2})(?:0[48]|[2468][048]|[13579][26])%s(?:02%s29)' % (ymd_symbol, ymd_symbol)
# 四百年再闰
pattern_ymd5 = r'(?:0[48]|[2468][048]|[13579][26])(?:00)%s(?:02%s29)' % (ymd_symbol, ymd_symbol)
# 5类取或
return '(?:%s)|(?:%s)|(?:%s)|(?:%s)|(?:%s)' % (
pattern_ymd1, pattern_ymd2, pattern_ymd3, pattern_ymd4, pattern_ymd5)
def time24(hms_symbol=':'):
return r'(?:[01]\d|2[0-3])%s(?:[0-5]\d)%s(?:[0-5]\d)' % (hms_symbol, hms_symbol)
def time12(hms_symbol=':'):
return r'(?:0\d|1[0-2])%s(?:[0-5]\d)%s(?:[0-5]\d)' % (hms_symbol, hms_symbol)
if __name__ == '__main__':
try:
time_str = find(date_time24(), '检索这段2020-01-31 23:59:59文字中首次出现的日期时间')
print(time_str)
time_str = find(date(''), '这段20200231文字中的日期不合法')
print(time_str)
except:
traceback.print_exc()