Skip to main content
Regexflux

ISO 8601 Duration

intermediate

Matches ISO 8601 duration strings like P1Y2M3DT4H5M6S.

durationisoperiodtime

Pattern

/P(?:\d+Y)?(?:\d+M)?(?:\d+W)?(?:\d+D)?(?:T(?:\d+H)?(?:\d+M)?(?:\d+(?:\.\d+)?S)?)?/g

Try It

0 of 8 strings matched

Explanation

Matches ISO 8601 duration format: P followed by optional year, month, week, day components, then T with optional hour, minute, second (with fractional) components.

Test Strings

Matching

  • P1Y2M3D
  • PT1H30M
  • P1DT12H
  • PT0.5S

Non-matching

  • 1Y2M
  • T1H30M
  • duration
  • 12345

Language Compatibility

LanguageSupport
JSFull support
PYTHONFull support
JAVAFull support
PHPFull support
GOFull support

Code Snippets

const regex = /P(?:\d+Y)?(?:\d+M)?(?:\d+W)?(?:\d+D)?(?:T(?:\d+H)?(?:\d+M)?(?:\d+(?:\.\d+)?S)?)?/g;
const text = "your text here";
const matches = text.match(regex);
console.log(matches);

Related Patterns