Regex failing with text conversion to days – Python 3.10.x


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, 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?

Asked By: artemis



You could try the following regex (Demo):

(?:(d+) Days?)?(?: ?(d+) Hours?)?(?: ?(d+) Minutes?)?


  • (?:...) marks a non-capturing group
  • (...) marks a captured group
  • ? after a symbol or group means it is optional
  • d+ means one or more digits (0123…)

Sample Python implementation:

import re

_DHM_RE = re.compile(r'(?:(d+) Days?)?(?: ?(d+) Hours?)?(?: ?(d+) Minutes?)?')

def parse_dates(s: str) -> int:
    m =
    if m is None:
        return 0

    days = int( or 0)
    hours = int( or 0)
    minutes = int( or 0)

    days += hours / _HOURS_IN_DAY
    days += minutes / _MINUTES_IN_DAY

    return int(days)

strings = """
142 Days 16 Hours
128 Days 9 Hours 43 Minutes
10 Minutes
52 Hours

for s in strings:
    d = parse_dates(s)
    print(f'{s!r} has {d} days.')
Answered By: rv.kvetch

Here’s a way to do it:

import re
a = ['142 Days 16 Hours', '128 Days 9 Hours 43 Minutes', '10 Minutes']
def parse_dates(data):
    x = ['(d+)s' + unit, data) for unit in ['Day', 'Hour', 'Minute']]
    x = [0 if y is None else int( for y in x]
    return x[0] + x[1] / 24 + x[2] / 1440
[print(parse_dates(data)) for data in a]


Answered By: constantstranger
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.