Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^\d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}:\d{2}(?:\.\d{1,6})?(?:Z|[+-]\d{2}:?\d{2})$

Matches ISO 8601 datetime strings with optional fractional seconds and timezone offset or 'Z' suffix, starting from the year.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string 2023-...
\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) 15
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) 45
(?:\.\d{1,6})? Non-capturing group: optional fractional seconds (1-6 digits) .123456
(?:Z|[+-]\d{2}:?\d{2}) Non-capturing group: 'Z' or timezone offset (+/-HH:MM or +/-HHMM) Z or +05:30
$ Anchor: asserts position at end of the string ...00Z

Matches

  • 2023-01-15T14:30:45Z
  • 2023-12-31T23:59:59+05:30
  • 2023-02-28T00:00:00-0800
  • 2023-07-04T12:00:00.123456-04:00
  • 1999-12-31T23:59:59.9Z

Does not match

  • 2023-1-15T14:30:45Z
  • 2023-01-5T14:30:45Z
  • 2023-01-15T14:30:45
  • 2023-01-15T14:30:45.1234567Z
  • 2023-01-15T14:30:45+5:30
  • 2023-01-15T14:30:45Z
  • 2023-01-15T14:30:45Z

Common pitfalls

  • Forgetting to escape backslashes in Python strings (use raw strings: r'...').
  • Assuming the pattern matches all valid ISO 8601 formats (e.g., week-based dates like '2023-W01-1' are not matched).
  • Overlooking that the timezone offset must be exactly 2 digits for hours and minutes (e.g., '+5:30' fails).
  • Not accounting for the optional fractional seconds (e.g., '2023-01-15T14:30:45Z' is valid).
  • Assuming the pattern matches dates before 1000 AD or after 9999 AD (year is exactly 4 digits).

Performance notes

The pattern is linear in complexity (no nested quantifiers or backtracking risks) and is efficient for typical use cases. Anchors (^ and $) ensure no partial matches, improving reliability.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Does this pattern match all ISO 8601 datetime strings?

No. It matches a subset of ISO 8601 datetime strings with the format YYYY-MM-DDTHH:MM:SS[.ffffff][Z|±HH:MM]. It does not support week-based dates, ordinal dates, or reduced accuracy formats.

Why is the fractional seconds group optional?

ISO 8601 allows datetime strings without fractional seconds (e.g., '2023-01-15T14:30:45Z'). The group (?:"\."\d{1,6})? makes this optional.

Can I use this pattern to validate user input for a datetime field?

Yes, but consider additional validation for business logic (e.g., valid dates like February 30th). This pattern only checks the string format.

How do I make the pattern case-insensitive for 'T'?

Add the (?i) flag at the start of the pattern (e.g., r'(?i)^\d{4}...'). However, 'T' is typically uppercase in ISO 8601, so this is rarely needed.