Python (re)

US Date Format (MM/DD/YYYY) in PY

Match US-style dates in MM/DD/YYYY format with range validation.

Try it in the PY tester →

Pattern

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

Python (re) code

pyPython
import re

pattern = re.compile(r"(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4}")
input_text = "01/15/2024"
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

Month alternation covers 01-12, day covers 01-31, year is any 4-digit number. Slashes are literal separators escaped with \.

Examples

Input

01/15/2024

Matches

  • 01/15/2024

Input

12/31/1999

Matches

  • 12/31/1999

Input

13/01/2024

No match

Same pattern, other engines

← Back to US Date Format (MM/DD/YYYY) overview (all engines)