Regular Expressions 101

Community Patterns

Full ISO 8601 Duration with lookaround

0

Regular Expression
PCRE (PHP <7.3)

/
^P(?!$)(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?=\d)(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$
/
gm

Description

With lookaround, this regular expression will reject P and PT as invalid duration strings. If your library does not support lookaround, I recommend that you use Full ISO 8601 Duration without lookaround (also in the library) instead, with the caveat that you will need to verify the string is not P or PT, which will be incorrectly accepted.

Submitted by anonymous - 4 years ago