import re
#删除字符串中的中文字符
def subChar(str):
match=re.compile(u'[\u4e00-\u9fa5]')
return match.sub('',str)
#提取日期
def extractDate(str):
if not str:
return None
raw=subChar(str)
if not raw:
return None
#提取前10位字符
rawdate=raw[:10]
datelist=re.findall("\d+",rawdate)
if not datelist:
return None
if datelist.__len__()==3:
if (float(datelist[0])>2099 or float(datelist[0])<1900) or float(datelist[1])>12 or float(datelist[2])>31:
return None
else:
return '-'.join(datelist)
if datelist.__len__()==2:
if (float(datelist[0])>2099 or float(datelist[0])<1900) or float(datelist[1])>12:
return None
else:
datelist.append('01')
return '-'.join(datelist)
if datelist.__len__()==1:
if float(datelist[0])>20991231 or float(datelist[0])<19000101:
return None
else:
return datelist[0]
return None
本文章为转载,原文出处:http://blog.csdn.net/weisongming/article/details/78497909