-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch07_date_detection.py
53 lines (42 loc) · 1.39 KB
/
ch07_date_detection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
### Chapter 7 Project: Date Detection
#detect and validate dates in DD/MM/YYYY format
import re
dateRegex = re.compile(r'''(
(([0][1-9])|([1-2]\d)|([3][0-1])) #DD 01-31
(/)
([0][1-9]|[1][1-2]) #MM 01-12
(/)
([1-2]\d{3}) #YYYY 1000-2999
)''', re.VERBOSE)
text = '29/02/1924, 31/02/1900, 30/02/2000, 30/04/1800, 31/02/2000, 31/04/2400, 23/02/2000'
matches = []
validDates = []
invalidDates = []
#take found dates, validates, and creates list of validated dates and invalid dates
for groups in dateRegex.findall(text):
#if year divisible by 4 and 400
leapYear = False
if int(groups[8]) % 4 == 0:
if int(groups[8]) % 100 == 0:
if int(groups[8]) % 400 == 0:
leapYear = True
else:
leapYear = True
if groups[6] == '04' or groups[6] == '06' or groups[6] == '09' or groups[6] == '11':
if int(groups[1]) > 30:
invalidDates.append(groups[0])
continue
if groups[6] == '02':
if leapYear == False and int(groups[1]) > 28:
invalidDates.append(groups[0])
continue
elif leapYear == True and int(groups[1]) > 29:
invalidDates.append(groups[0])
continue
validDates.append(groups[0])
if len(validDates) > 0:
print(validDates)
if len(invalidDates) > 0:
print(invalidDates)
else:
print('No matches of dates')