Python (re)

ISO 8601 Date in PY

Match dates in ISO 8601 format: YYYY-MM-DD with valid month (01–12) and day (01–31) ranges.

Try it in the PY tester →

Pattern

regexPY
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])")
input_text = "2024-01-15"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

Year is any 4-digit number. Month uses alternation to enforce 01-12. Day enforces 01-31, though it won't catch month-specific overflows (e.g. Feb 30).

Examples

Input

2024-01-15

Matches

  • 2024-01-15

Input

1999-12-31

Matches

  • 1999-12-31

Input

2024-13-01

No match

Same pattern, other engines

← Back to ISO 8601 Date overview (all engines)