I have a list of time durations in text, for example, ['142 Days 16 Hours', '128 Days 9 Hours 43 Minutes', '10 Minutes']
I need to build a function to take these durations and instead come up with the total number of days.
The specific text could be a single day, days and hours, hours and minutes, a single set of minutes, or a day, hour, and minute.
I have tried the following:
def parse_dates(data):
days = int(re.match(r'\d+\sDay', data)[0].split(' ')[0]) if re.match(r'\d+\sDay', data) is not None else 0
hours = int(re.match(r'\d+\sHour', data)[0].split(' ')[0]) if re.match(r'^\d+Hour*s$', data) is not None else 0
minutes = int(re.match(r'\d+\sMinute', data)[0].split(' ')[0]) if re.match(r'\d+\sMinute', data) is not None else 0
days += hours / 24
days += minutes / 1440
return days
The provided function fails regardless of using re.match()
or re.search()
, leading me to believe there is a problem with the expression itself.
However, the hours and minutes are ALWAYS showing as 0. How can I fix my regex
, or devise a better solution, to parse these files appropriately?