Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of '2023-05-15T14:30:45Z' |
\d{4} |
Exactly 4 digits (year) | 2023 |
- |
Literal hyphen character | - |
\d{1,2} |
1 or 2 digits (month) | 05 |
- |
Literal hyphen character | - |
\d{1,2} |
1 or 2 digits (day) | 15 |
T |
Literal 'T' character separating date and time | T |
\d{1,2} |
1 or 2 digits (hour in 24-hour format) | 14 |
: |
Literal colon character | : |
\d{2} |
Exactly 2 digits (minutes) | 30 |
: |
Literal colon character | : |
\d{2} |
Exactly 2 digits (seconds) | 45 |
(?:\.\d{1,6})? |
Non-capturing group: optional fractional seconds (1 to 6 digits) preceded by a dot | .123456 or .1 |
(?:Z|[+-]\d{2}:?\d{2})? |
Non-capturing group: optional timezone indicator (Z for UTC or ±HH:MM/HHMM) | Z or +02:00 or -0500 |
$ |
Anchor: asserts position at end of the string | End of '2023-05-15T14:30:45Z' |
Matches
2023-05-15T14:30:45Z2023-5-5T9:5:5.123456+02:001999-12-31T23:59:59.999999-05000001-01-01T00:00:00Z
Does not match
2023/05/15T14:30:45Z2023-05-15 14:30:45Z2023-05-15T14:30:452023-05-15T14:30:45.1234567Z2023-05-15T25:00:00Z2023-05-15T14:30:45+2:00
Common pitfalls
- Timezone offset without colon (e.g., +0200) is not matched due to the optional colon in the pattern
- Month or day with leading zero is required (e.g., '5' is valid but '05' is also valid)
- Fractional seconds must be 1 to 6 digits; longer sequences are not matched
- The pattern does not validate logical date/time values (e.g., '2023-13-01' is matched but invalid)
- The pattern allows single-digit hours, minutes, or seconds (e.g., '9:5:5'), which may not be desired in some contexts
Performance notes
The pattern is linear and does not contain nested quantifiers or alternations that could cause catastrophic backtracking. Anchors (^ and $) ensure the entire string is checked efficiently. Performance is generally good for typical ISO 8601 strings.