I'm trying to match a time format. The time can be written with optional hours, minutes and milliseconds. The only required value is seconds. So all of these times are correctly written and should match.
10:11:12.123 '10 hours, 11 minutes, 12 seconds and 123 milliseconds
10:11:12 '10 hours, 11 minutes and 12 seconds
11:12.123 '11 minutes, 12 seconds and 123 milliseconds
11:12 '11 minutes and 12 seconds
12.123 '12 seconds and 123 milliseconds
12 '12 seconds
My regex looks like this: (?:(?:(\d*):)?(?:(\d*):)?(\d*)(?:\.(\d*))?)
I would like each group to correspond to the right value. So group 1 should always return hours, group 2 should always return minutes, etc... I tried this in Regex101 but as you can see in those results group 1 is always preferred over group 2 when there are no hours in play. It matches the time 11:12 perfectly but it places 11 in group 1 telling me it's hours while it actually is minutes. That's no strange behavior because the minutes in this string matches group 1. But it is not what I want.
How to make sure that each group always corresponds to the right part of the time? Can I prioritize group 2 over group 1 in some way?