import re
from datetime import datetime
test_date = 'His birthday is 2016-12-12 14:34, he is a cute baby. Erbao's birthday is 2016-12-21 11:34, so cute.'
test_datetime = ' His birthday is 2016-12-12 14:34, he is a cute baby. Erbao's birthday is 2016-12-21 11:34, so cute.'
# date
mat = re.search(r"(\ d{4}-\d{1,2}-\d{1,2})",test_date)
print mat.groups()
# ('2016-12-12',)
print mat.group(0)
# 2016-12-12
date_all = re.findall(r"(\d{4}-\d{1,2}-\d{1,2})",test_date)
for item in date_all:
print item
# 2016- 12-12
# 2016-12-21
# datetime
mat = re.search(r"(\d{4}-\d{1,2}-\d{1,2}\s\d{1,2} :\d{1,2})",test_datetime)
print mat.groups()
# (' 2016-12-12 14:34',)
print mat.group(0)
# 2016-12-12 14:34
date_all = re.findall(r"(\d{4}-\d{1,2}-\d{1,2}\s\d {1,2}:\d{1,2})",test_datetime)
for item in date_all:
print item
# 2016-12-12 14:34
# 2016-12-21 11:34
##Valid time
# like this The date 2016-12-35 can also be matched. The test is as follows.
test_err_date = 'The date 2016-12-35 can also be matched. The test is as follows.'
print re.search(r"(\d{4}- \d{1,2}-\d{1,2})",test_err_date).group(0)
# 2016-12-35
# You can add a judgment
def validate(date_text):
try:
if date_text != datetime. strptime(date_text, "%Y-%m-%d").strftime('%Y-%m-%d'):
raise ValueError
return True
except ValueError:
# raise ValueError("Error is date format or date, format is year-month-day")
return False
print validate(re.search(r"(\d{4}-\d{1,2}-\d{ 1,2})",test_err_date).group(0))
# false
# Other formats match. Such as 2016-12-24 and 2016/12/24 date format.
date_reg_exp = re.compile('\d{4} [-/]\d{2}[-/]\d{2}')
test_str= """
Christmas Eve 2016-12-24 is different from last year's 2015/12/24.
" ""
# Find all dates according to the regex and return
matches_list=date_reg_exp.findall(test_str)
# List and print the matching dates
for match in matches_list:
print match
# 2016-12-24
from datetime import datetime
test_date = 'His birthday is 2016-12-12 14:34, he is a cute baby. Erbao's birthday is 2016-12-21 11:34, so cute.'
test_datetime = ' His birthday is 2016-12-12 14:34, he is a cute baby. Erbao's birthday is 2016-12-21 11:34, so cute.'
# date
mat = re.search(r"(\ d{4}-\d{1,2}-\d{1,2})",test_date)
print mat.groups()
# ('2016-12-12',)
print mat.group(0)
# 2016-12-12
date_all = re.findall(r"(\d{4}-\d{1,2}-\d{1,2})",test_date)
for item in date_all:
print item
# 2016- 12-12
# 2016-12-21
# datetime
mat = re.search(r"(\d{4}-\d{1,2}-\d{1,2}\s\d{1,2} :\d{1,2})",test_datetime)
print mat.groups()
# (' 2016-12-12 14:34',)
print mat.group(0)
# 2016-12-12 14:34
date_all = re.findall(r"(\d{4}-\d{1,2}-\d{1,2}\s\d {1,2}:\d{1,2})",test_datetime)
for item in date_all:
print item
# 2016-12-12 14:34
# 2016-12-21 11:34
##Valid time
# like this The date 2016-12-35 can also be matched. The test is as follows.
test_err_date = 'The date 2016-12-35 can also be matched. The test is as follows.'
print re.search(r"(\d{4}- \d{1,2}-\d{1,2})",test_err_date).group(0)
# 2016-12-35
# You can add a judgment
def validate(date_text):
try:
if date_text != datetime. strptime(date_text, "%Y-%m-%d").strftime('%Y-%m-%d'):
raise ValueError
return True
except ValueError:
# raise ValueError("Error is date format or date, format is year-month-day")
return False
print validate(re.search(r"(\d{4}-\d{1,2}-\d{ 1,2})",test_err_date).group(0))
# false
# Other formats match. Such as 2016-12-24 and 2016/12/24 date format.
date_reg_exp = re.compile('\d{4} [-/]\d{2}[-/]\d{2}')
test_str= """
Christmas Eve 2016-12-24 is different from last year's 2015/12/24.
" ""
# Find all dates according to the regex and return
matches_list=date_reg_exp.findall(test_str)
# List and print the matching dates
for match in matches_list:
print match
# 2016-12-24
# 2015/12/24
-------Reference URL----https://www.cnblogs.com/OnlyDreams/p/7845527.html
------Author------'Randy'