Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | 2023-... |
\d{4} |
Exactly 4 digits (year) | 2023 |
- |
Literal hyphen separator | - |
(?:0[1-9]|1[0-2]) |
Non-capturing group for month: 01-09 or 10-12 | 01 |
- |
Literal hyphen separator | - |
(?:0[1-9]|[12][0-9]|3[01]) |
Non-capturing group for day: 01-09, 10-29, or 30-31 | 15 |
T |
Literal 'T' separator between date and time | T |
(?:[01][0-9]|2[0-3]) |
Non-capturing group for hour: 00-19 or 20-23 | 14 |
: |
Literal colon separator | : |
[0-5][0-9] |
Exactly 2 digits for minutes: 00-59 | 30 |
: |
Literal colon separator | : |
[0-5][0-9] |
Exactly 2 digits for seconds: 00-59 | 45 |
(?:\.\d{1,3})? |
Optional non-capturing group for fractional seconds: .1 to .999 | .123 |
(?:Z|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9]) |
Non-capturing group for timezone: 'Z' or ±HH:MM | Z or +05:30 |
$ |
Anchor: asserts position at end of the string | ...30Z |
Matches
2023-01-15T14:30:45Z2023-12-31T23:59:59.999+05:301999-02-28T00:00:00.1Z2000-01-01T00:00:00-12:00
Does not match
2023-13-01T00:00:00Z2023-01-32T00:00:00Z2023-01-01T24:00:00Z2023-01-01T12:60:00Z2023-01-01T12:00:60Z2023-01-01T12:00:00.1234Z2023-01-01T12:00:00+24:00
Common pitfalls
- Invalid months (e.g., 13) or days (e.g., 32) are not matched
- Timezone offsets must be in ±HH:MM format; ±HHMM is not supported
- Fractional seconds must be 1-3 digits; more digits are not matched
- Hour must be 00-23; 24 is invalid
- Seconds must be 00-59; 60 is invalid
Performance notes
The regex is linear in complexity with no nested quantifiers, so catastrophic backtracking is not a concern. Anchors (^ and $) ensure full-string matching, which is efficient.