Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of '2023-01-01T12:00:00Z' |
\d{4} |
Exactly 4 digits (year) | 2023 |
- |
Literal hyphen character | - |
\d{1,2} |
1 or 2 digits (month) | 01 |
- |
Literal hyphen character | - |
\d{1,2} |
1 or 2 digits (day) | 01 |
T |
Literal 'T' character separating date and time | T |
\d{1,2} |
1 or 2 digits (hour in 24-hour format) | 12 |
: |
Literal colon character | : |
\d{2} |
Exactly 2 digits (minutes) | 00 |
: |
Literal colon character | : |
\d{2} |
Exactly 2 digits (seconds) | 00 |
(?:\.\d{1,6})? |
Non-capturing group: optional fractional seconds (1 to 6 digits) preceded by a dot | .123456 |
(?:Z|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9])? |
Non-capturing group: optional timezone, either 'Z' or ±HH:MM where HH is 00-23 and MM is 00-59 | Z or +05:30 |
$ |
Anchor: asserts position at end of the string | End of '2023-01-01T12:00:00Z' |
Matches
2023-01-01T12:00:00Z2023-12-31T23:59:59.999999+05:301999-01-01T00:00:00-12:002000-02-29T00:00:00.1Z
Does not match
2023-1-1T12:00:00Z2023-01-01T12:00:002023-01-01T12:00:00.1234567Z2023-01-01T24:00:00Z2023-01-01T12:00:00+24:002023-01-01T12:00:00.123456Zextra
Common pitfalls
- Forgetting the ^ and $ anchors can lead to partial matches within longer strings
- The month/day/hour/minute/second components must be zero-padded to 2 digits where applicable (e.g., '01' not '1')
- The timezone offset must be in the format ±HH:MM with valid hour (00-23) and minute (00-59)
- Fractional seconds must not exceed 6 digits
Performance notes
The pattern is linear and does not contain nested quantifiers that could cause catastrophic backtracking. The use of non-capturing groups (?:...) improves performance by avoiding unnecessary capture overhead.