I'm attempting to write Regex to match the ISO8601 standard for temporal durations, but only where PT and M or S are valid.
So far I have found a full example of ISO8601 regex but it is more complex than I need. I need match durations like the following:
Essentially I want the Regex to always check that:
PT is at the beginning of the stringMy attempt so far:
^PT[0-9]+M - except this allows something like 10.5M because the 5M countsI'm really stuck on trying to figure this out, I've been trying to get each part to match so I could try and combine them all later but I can't get over the first hurdle.
Shouldn't be much more complex than
const rxIso8601Duration = /^PT((([0-9]+M)?([0-9]+S))|([0-9]+M))$/ ;
Breaking it down:
^ // - anchor the match at start-of-text, followed by
PT // - the literal PT, followed by
( // - a group, consisting of either
( // - a group, consisting of
([0-9]+M)? // - an optional minutes designation, followed by
([0-9]+S) // - a required seconds designation
) //
| // or
([0-9]+M) // - a required minutes designation
) // the whole of which is followed by
$ // end-of-text
If you want to allow fractional minutes or seconds, Just change the appropriate sub-expression(s):
([0-9]+M)123M, but not 123.456M([0-9]+(\.[0-9]+)?M)123M but also matches 123.456M