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) | .123456 |
(?:Z|[+-]([01][0-9]|2[0-3]):([0-5][0-9]))? |
Non-capturing group: optional timezone (Z or ±HH:MM) | 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+01:002023-01-01T00:00:00-12:002023-06-15T12:34:56.123456Z2023-06-15T12:34:56.1Z2023-06-15T12:34:56+05:30
Does not match
2023/10/05T14:30:00Z2023-10-05 14:30:00Z2023-10-05T14:30:002023-10-05T24:00:00Z2023-13-01T12:00:00Z2023-10-05T14:30:00+25:002023-10-05T14:30:00.1234567Z2023-10-05T14:30:00.123Z123
Common pitfalls
- Forgetting to escape backslashes in Python strings (use raw strings: r'...')
- Assuming the pattern validates actual date/time values (e.g., '2023-02-30' is syntactically valid but invalid logically)
- Overlooking that the timezone group is optional, leading to partial matches
- Not accounting for single-digit months/days/hours (e.g., '2023-1-5T1:3:0Z' is valid)
- Misunderstanding that the fractional seconds group is non-capturing (?:...)
Performance notes
The pattern is linear in complexity with no nested quantifiers, so catastrophic backtracking is unlikely. Anchors (^ and $) ensure full-string matching, which is efficient. For very large inputs, pre-filtering with a simpler pattern (e.g., ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}) may improve performance.