Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE2 (PHP >=7.3)

/
^(P((?<numYears>[1-9]+[0-9]*)Y)?((?<numMonths>[1-9]+[0-9]*)M)?((?<numDays>[1-9]+[0-9]*)D)?(T((?<numHours>[1-9]+[0-9]*)H)?((?<numMinutes>[1-9]+[0-9]*)M)?((?<numSeconds>[1-9]+[0-9]*)S)?)?|P(?<numWeeks>[1-9]+[0-9]*)W)$
/
gm

Description

Duration regular expression as per https://en.wikipedia.org/wiki/ISO_8601 PnYnMnDTnHnMnS or PnW

Examples: P1Y1M1DT1H1M1S P1M - 1 month PT1M - 1 minute P1MT1M - 1 month and 1 minute P1W - 1 week P1D - 1 day PT2S - 2 seconds

Submitted by Sharath Malladi - 7 months ago (Last modified 7 months ago)