Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of '2023-12-31T23:59:59Z' |
([0-9]{4}) |
Group 1: Four-digit year (0-9) | 2023 |
- |
Literal hyphen character | - |
(0[1-9]|1[0-2]) |
Group 2: Two-digit month (01-12) | 12 |
- |
Literal hyphen character | - |
(0[1-9]|[12][0-9]|3[01]) |
Group 3: Two-digit day (01-31) | 31 |
T |
Literal 'T' separator | T |
([01][0-9]|2[0-3]) |
Group 4: Two-digit hour (00-23) | 23 |
: |
Literal colon character | : |
([0-5][0-9]) |
Group 5: Two-digit minute (00-59) | 59 |
: |
Literal colon character | : |
([0-5][0-9]|60) |
Group 6: Two-digit second (00-59 or 60 for leap second) | 59 |
(\.[0-9]{1,6})? |
Group 7: Optional fractional seconds (1-6 digits) | .123456 |
(Z|[+-]([01][0-9]|2[0-3]):([0-5][0-9]))? |
Group 8: Optional timezone (Z or ±HH:MM) | Z or +05:30 |
$ |
Anchor: asserts position at end of the string | End of '2023-12-31T23:59:59Z' |
Matches
2023-12-31T23:59:59Z2000-01-01T00:00:00+00:001999-02-28T12:34:56.123456-05:002020-02-29T23:59:60Z
Does not match
2023-13-01T00:00:00Z2023-12-32T00:00:00Z2023-12-31T24:00:00Z2023-12-31T23:60:00Z2023-12-31T23:59:592023-12-31T23:59:59.1234567Z2023-12-31T23:59:59+24:00
Common pitfalls
- Leap seconds (60) are allowed but may not be supported by all systems.
- The regex does not validate the actual calendar date (e.g., February 30 is not caught).
- Timezone offsets must be in ±HH:MM format; ±H:MM or ±HHMM are not matched.
- Fractional seconds must be 1-6 digits; 0 digits or 7+ digits are not matched.
- The 'T' separator is required; spaces or other separators are not matched.
Performance notes
The pattern is linear in complexity with no nested quantifiers, so catastrophic backtracking is not a concern. Anchors (^ and $) ensure full-string matching, which is efficient.