I have an app that give a duration in the format Days Hours Minutes and I need to calculate the number of mins from the string.
Examples of the string;
2 Days 12 Hours 5 Minutes
12 Days 1 Hour 1 Minute
1 Hour 35 Minutes
1 Day 1 Minute
35 Minutes
Solved! Go to Solution.
Went with regex parse here - find the numbers before "space minute" (case insensitive in case minute is not capitalized and minute in order to catch both minute and minutes
EDIT: If there are duplicate spaces between or no spaces at all the expression can be adjusted to have a * following the \s.
I need to work out the mins, i.e.
2 Days 12 Hours 5 Minutes = 2 *24*60 + 12*60 + 5 = 3605 mins
Assuming you'll always have minutes, this regex should work
(\d*(?=\sd))?\D*?(\d*(?=\sh))?\D*?(\d*(?=\sm))\D*?
\D here being non-digit character
and ?= is using the lookahead to keep things in order to the first group is digits followed by a d for days etc.