Regex Guide · Prompt: “Match YYYY-MM-DD dates with valid month 01-12 and day 01-31 captured as groups”

Validate YYYY-MM-DD ISO Dates with Regular Expressions

Verified by LiveFormulaLab Engineering Team • Updated November 2026

Validate and extract strict YYYY-MM-DD calendar dates with Regex, rejecting invalid months (>12) and days (>31). Test live.

Verified Regex Syntax

(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])

How This Formula Works (Step-by-Step)

A naive \d{4}-\d{2}-\d{2} pattern accidentally accepts invalid dates like 2026-19-45.

Step 1: (0[1-9]|1[0-2]) restricts months strictly to 01 through 12.

Step 2: (0[1-9]|[12]\d|3[01]) restricts days strictly to 01 through 31 while capturing Year, Month, and Day separately.

Formula Syntax & Arguments Breakdown

Enforces exact 4-digit years, valid 01–12 month pairs, and valid 01–31 day pairs separated by literal hyphens while capturing Year, Month, and Day as three structured groups for validation or reformatting.

Argument / ClauseRequirementData TypeRole & Behavior
(\d{4})RequiredCapture Group 1 (Year)Matches exactly four numeric year digits (for example, 2026).
-RequiredLiteral SeparatorRequires strict ISO-8601 hyphen separators between year, month, and day segments.
(0[1-9]|1[0-2])RequiredCapture Group 2 (Month)Restricts month values strictly to 01–09 or 10–12, rejecting 00 and 13+.
(0[1-9]|[12]\d|3[01])RequiredCapture Group 3 (Day)Restricts day numbers strictly to 01–09, 10–29, or 30–31.

3 Real-World Use Cases

  • Extracting timestamps from structured release notes and deployment logs.
  • Validating API query date parameters before hitting SQL databases.
  • Parsing ISO dates out of filenames like report-2026-11-23.csv.

Cross-Platform Equivalents

Excel
=REGEXEXTRACT(A2, "\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])")
SQL
SELECT log_line FROM events WHERE log_line ~ '\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])';
Python Pandas
df['iso_date'] = df['text'].str.extract(r'(\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01]))')

Common Mistakes & Pro Tips

Pitfalls & How to Fix Them

  • Mistake: Using simple \d{4}-\d{2}-\d{2} silently accepts bogus calendar dates like 2026-99-45.

    Fix: Constrict month and day digit branches explicitly with (0[1-9]|1[0-2]) and (0[1-9]|[12]\d|3[01]).

  • Mistake: Testing 2026-02-31 passes format checks because regex does not track 28/30-day months or leap years.

    Fix: Follow regex format matching with a Date object / DATEVALUE parse check for true calendar validity.

  • Mistake: Unbounded patterns match substrings inside longer IDs such as 992026-01-018.

    Fix: Surround the pattern with word boundaries \b...\b or ^...$ anchors for standalone cells.

Engineering Pro Tips

  • Reorder capture groups via replacement "$2/$3/$1" to convert ISO dates into US MM/DD/YYYY strings.
  • Append (?:T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z?)? to match full ISO-8601 timestamps optionally.
  • Use non-capturing (?:...) inside the month/day branches when extracting into Excel/Sheets if you only want the full date string.
  • Wrap extracted ISO strings in DATEVALUE() inside Excel or Google Sheets to convert text dates into sortable numeric dates.

Frequently Asked Questions

Does this regex check leap-year rules like Feb 29?
Regex validates format and 01-31 ranges cleanly; use a native Date parser afterward for calendar leap-year checks.
How do I prevent matching partial longer numbers like 12026-01-01?
Wrap the pattern with word boundaries: \b(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])\b.
Can I make the separator allow slashes or hyphens?
Replace - with [-/] (or use backreference ([-/])...\2 so both separators match).

Related Formulas & Guides