Regex to Match ISO 8601 Duration Format
Validate ISO 8601 duration strings like P1Y2M3DT4H5M6S. Matches period designators for years, months, weeks, days, hours, minutes, and seconds.
Regular Expression
/^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(?:T(?!$)(\d+H)?(\d+M)?(\d+(?:\.\d+)?S)?)?$/
Token Breakdown
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| P | Matches the literal character 'P' |
| (?! | Start of negative lookahead assertion |
| $ | Anchors at the end of the string (or line in multiline mode) |
| ) | End of group |
| ( | Start of capturing group |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| Y | Matches the literal character 'Y' |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| ( | Start of capturing group |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| M | Matches the literal character 'M' |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| ( | Start of capturing group |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| W | Matches the literal character 'W' |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| ( | Start of capturing group |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| D | Matches the literal character 'D' |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| (?: | Start of non-capturing group |
| T | Matches the literal character 'T' |
| (?! | Start of negative lookahead assertion |
| $ | Anchors at the end of the string (or line in multiline mode) |
| ) | End of group |
| ( | Start of capturing group |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| H | Matches the literal character 'H' |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| ( | Start of capturing group |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| M | Matches the literal character 'M' |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| ( | Start of capturing group |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| (?: | Start of non-capturing group |
| \. | Matches a literal dot |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| S | Matches the literal character 'S' |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex validates ISO 8601 duration format strings. Here is the token-by-token breakdown:
^ — Anchors the match at the start of the string.
P — Matches the literal P (period) character that begins every ISO 8601 duration. This is mandatory and stands for period.
(?!$) — A negative lookahead ensuring the string does not end immediately after P. A bare P is not a valid duration.
(\d+Y)? — Optional capturing group 1 matches the years component: one or more digits followed by Y.
(\d+M)? — Optional capturing group 2 matches the months component: digits followed by M.
(\d+W)? — Optional capturing group 3 matches the weeks component: digits followed by W.
(\d+D)? — Optional capturing group 4 matches the days component: digits followed by D.
(?:T(?!$) — An optional non-capturing group for the time portion, starting with the literal T time designator. The negative lookahead (?!$) ensures T is not the last character.
(\d+H)? — Optional capturing group 5 matches hours: digits followed by H.
(\d+M)? — Optional capturing group 6 matches minutes in the time section: digits followed by M.
(\d+(?:.\d+)?S)? — Optional capturing group 7 matches seconds: digits with an optional decimal fraction followed by S.
)? — Makes the entire time section optional.
$ — Anchors at the end of the string.
ISO 8601 durations are used in APIs, scheduling systems, video metadata, and configuration files. Examples include P1Y (one year), PT1H30M (1 hour 30 minutes), and P2DT12H (2 days 12 hours).
Example Test Strings
| Input | Expected |
|---|---|
| P1Y2M3DT4H5M6S | Match |
| PT1H30M | Match |
| P1D | Match |
| P | No Match |
| 1Y2M | No Match |
| P3W | Match |
Try It — Interactive Tester
78 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match ISO 8601 DateTime
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d{1,3})?(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/
Regex to Match Relative Date Expressions
/(\d+)\s+(second|minute|hour|day|week|month|year)s?\s+(ago|from now|later|earlier)/gi
Regex to Match Cron Expression Format
/^(?:[0-9*,/\-]+\s+){4}[0-9*,/\-]+$/
Regex to Match Timezone Offsets
/^(?:Z|[+-](?:0[0-9]|1[0-4]):[0-5][0-9])$/