Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of string |
\d{4} |
Exactly 4 digits (0-9) | 2023 |
- |
Literal hyphen character | - |
\d{2} |
Exactly 2 digits (0-9) | 12 |
- |
Literal hyphen character | - |
\d{2} |
Exactly 2 digits (0-9) | 31 |
T |
Literal 'T' character separating date and time | T |
\d{2} |
Exactly 2 digits (0-9) for hours | 23 |
: |
Literal colon character | : |
\d{2} |
Exactly 2 digits (0-9) for minutes | 59 |
: |
Literal colon character | : |
\d{2} |
Exactly 2 digits (0-9) for seconds | 59 |
(?:\.\d{1,6})? |
Non-capturing group: optional fractional seconds (1 to 6 digits) | .123456 or .1 |
(?:Z|[+-]\d{2}:\d{2}) |
Non-capturing group: either 'Z' for UTC or timezone offset in ±HH:MM format | Z or +05:30 or -08:00 |
$ |
Anchor: asserts position at end of the string | End of string |
Matches
2023-12-31T23:59:59Z2023-12-31T23:59:59.123456Z2023-12-31T23:59:59+05:302023-12-31T23:59:59-08:001970-01-01T00:00:00Z
Does not match
2023-12-31T23:59:592023-12-31T23:59:59.1234567Z2023-12-31T24:00:00Z2023-12-31T23:59:59+25:002023-12-31T23:59:59.123456+05:30
Common pitfalls
- Omitting the 'T' separator between date and time
- Using more than 6 fractional digits
- Incorrect timezone offset format (e.g., +5:30 instead of +05:30)
- Forgetting to escape the hyphen in character classes (though not needed here)
- Assuming this matches all valid ISO 8601 datetimes (e.g., it does not match '2023-12-31 23:59:59Z')
Performance notes
The pattern is linear and does not contain nested quantifiers or alternations that could lead to catastrophic backtracking. Anchors (^ and $) ensure efficient matching by preventing partial matches. No significant performance concerns.