Advertisement

Pattern library / Dates & Time

Regex to Match Dates in YYYY-MM-DD Format

\d{4}-\d{2}-\d{2} looks right and mostly is — but left loose it happily matches 9999-99-99 and the middle of a sentence. This is the full-match version with sane range limits, and it is valid RE2, so it works in GA4, BigQuery, Go and Rust as well as JavaScript.

The pattern
^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
2026-10-28Matchvalid date, full matchTry →
1999-01-31Matchmonth 01, day 31Try →
2026-13-01No matchmonth 13 rejectedTry →
2026-00-10No matchmonth 00 rejectedTry →
2026-10-32No matchday 32 rejectedTry →
order 2026-10-28 shippedNo matchanchors force full matchTry →

Where to use it

JavaScript

const re = /^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/;
re.test('2026-10-28');  // true

Python

import re
re.match(r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$', '2026-10-28')

BigQuery / GA4 (RE2)

SELECT REGEXP_CONTAINS('2026-10-28', r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$')

Common mistakes

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

FAQ

Does this regex validate real calendar dates?

It validates the YYYY-MM-DD shape and range limits (month 01-12, day 01-31). Impossible dates like 2026-02-30 still match — confirm with a date parser in your language after matching.

How do I match a date inside a longer string?

Remove the ^ and $ anchors: [0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) finds the date anywhere in the text.

Does this work in Google Analytics 4?

Yes. The pattern uses only character classes, groups and alternation, all supported by the RE2 engine GA4 uses. Test it in the tester first, then paste it into your GA4 filter.

ISO 8601 DateTime Regex — Match & Test Online · Time Regex (HH:MM / HH:MM:SS) — 24-Hour Match · Extract Numbers from String — Regex + Examples

For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.