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|[+-]([01][0-9]|2[0-3]):([0-5][0-9]))?$

Matches ISO 8601 datetime strings with optional fractional seconds and timezone offset, from start to end of string.

Token-by-token breakdown

TokenMeaningExample
^ 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:00Z
  • 2023-1-5T1:3:0Z
  • 2023-12-31T23:59:59+01:00
  • 2023-01-01T00:00:00-12:00
  • 2023-06-15T12:34:56.123456Z
  • 2023-06-15T12:34:56.1Z
  • 2023-06-15T12:34:56+05:30

Does not match

  • 2023/10/05T14:30:00Z
  • 2023-10-05 14:30:00Z
  • 2023-10-05T14:30:00
  • 2023-10-05T24:00:00Z
  • 2023-13-01T12:00:00Z
  • 2023-10-05T14:30:00+25:00
  • 2023-10-05T14:30:00.1234567Z
  • 2023-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.

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 validate actual dates (e.g., February 30th)?

No. The pattern only checks the syntax (e.g., 4 digits for year, 1-2 digits for month/day). Logical validation (e.g., month ≤ 12, day ≤ 31) requires additional checks.

Why use non-capturing groups (?:...) instead of capturing groups (...)?

Non-capturing groups improve performance by avoiding unnecessary capture overhead. The timezone components are not needed for backreferences in this pattern.

Can this pattern match times with single-digit hours/minutes/seconds?

Yes. The quantifiers \d{1,2} allow 1 or 2 digits for hours, minutes, and seconds (e.g., '1:3:0' is valid).

Is the timezone offset required?

No. The entire timezone group (?:Z|...) is optional due to the trailing ? quantifier.

How would I modify this to require a timezone?

Remove the trailing ? from the timezone group: (?:Z|[+-]([01][0-9]|2[0-3]):([0-5][0-9]))