I have a free form time duration strings containing hour and minute values one of then could be optional
1 hour
12 hours 3 mins
47 mins
10 hours
1 min
I have to convert them to number of minutes. First searched for Python library which converts times and duration but the string format does not allow to use such approach
Then I tried with regex to extract the number groups:
re.search("(\d+)?.*(\d+\w)", string).group(1)
re.search("(\d+)?.*(\d+\w)", string).group(2)
which worked for most cases when hour and minute values are present or when only the minute value is present (since I made the first group optional) This regex fails when the hour is single digit (1 hour). Also because I am extracting only digits groups without the descriptive text (hour(s) and/or min(s) the calculation is wrong when there is only the hour value (with two digits) - like 10 hours and it wrongly is extracted as the 2nd group as minutes.
datetime.strptime()from python library datetime