Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of '2023-10-05T14:30:00Z' |
\d{4} |
Exactly 4 digits (year) | 2023 |
- |
Literal hyphen character | - |
\d{1,2} |
1 or 2 digits (month) | 10 |
- |
Literal hyphen character | - |
\d{1,2} |
1 or 2 digits (day) | 05 |
T |
Literal 'T' character (ISO 8601 separator) | 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) | 00 |
(?:\.\d{1,6})? |
Non-capturing group: optional fractional seconds (1-6 digits after dot) | .123456 |
(?:Z|[+-]\d{2}:\d{2})? |
Non-capturing group: optional timezone (Z for UTC or ±HH:MM offset) | Z or +02:00 |
$ |
Anchor: asserts position at end of the string | End of '2023-10-05T14:30:00Z' |
Matches
2023-10-05T14:30:00Z2023-1-5T1:3:0Z2023-12-31T23:59:59.999999+05:302000-01-01T00:00:00-12:001999-12-31T23:59:59.123456
Does not match
2023/10/05T14:30:00Z2023-10-05 14:30:00Z2023-10-05T25:00:00Z2023-10-05T14:30:002023-10-05T14:30:00.1234567Z2023-10-05T14:30:00+25:00
Common pitfalls
- Forgetting the ^ and $ anchors may cause partial matches (e.g., matching '2023-10-05T14:30:00Zextra').
- Assuming the pattern validates actual date/time values (e.g., '2023-13-01' would match but is invalid).
- Overlooking that the timezone offset must be exactly ±HH:MM (e.g., '+0530' without colon fails).
- Not accounting for optional fractional seconds (may expect them to always be present).
Performance notes
The pattern is linear and has no quantifier nesting, so catastrophic backtracking is not a concern. Anchors ensure full-string matching, which is efficient. For large datasets, pre-filter strings that don't start with 4 digits to improve performance.