Dates are the pattern everyone eventually needs: a form field to validate, a log file to search, a column of mixed text to clean up. They look simple, two digits, a slash, two digits, a slash, four digits, until a copied pattern accepts 99/99/9999 and you start again.

The reliable way is to build the pattern yourself, one piece at a time, testing as you go. This guide does that for the two formats met most often, DD/MM/YYYY and ISO YYYY-MM-DD, using the free Regex Tester on Ovatly, checking each pattern against strings that should pass and strings that should fail.

Contents

  • Quick answer
  • What a date pattern actually checks
  • Building the DD/MM/YYYY pattern, piece by piece
  • The ISO pattern: same pieces, different order
  • How to test a date pattern, step by step
  • Worked example: one test string, four verdicts
  • Extracting dates versus validating a field
  • Capturing the parts with groups
  • Limitations
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

For DD/MM/YYYY, use (0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\d{4}: a day from 01 to 31, a slash, a month from 01 to 12, a slash, and any four-digit year. For ISO dates, reorder the same pieces: \d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]). Test either pattern in the Regex Tester before trusting it. And hold on to the limit from the start: a regex checks the shape of a date, not the calendar. 31/02/2026 matches the pattern perfectly and is not a real date, and no pattern can handle leap years. Shape first, then a real date check in code.

What a date pattern actually checks

A date pattern is four small decisions: which two-digit combinations count as a day, which literal character separates the parts, which combinations count as a month, and how loose the year is, usually any four digits.

Writing those decisions separately is what separates a pattern you understand from a copied one. The loose version, \d{2}/\d{2}/\d{4}, checks only the skeleton and accepts 99/99/9999 without complaint. Each strict piece below removes one kind of nonsense. What no piece can remove is calendar nonsense: the pattern sees characters, not months with lengths.

Building the DD/MM/YYYY pattern, piece by piece

Start with the day. Three alternatives cover 01 to 31 exactly: 0[1-9] gives 01 to 09, [12][0-9] gives 10 to 29, and 3[01] gives 30 and 31. Joined with pipes inside a group: (0[1-9]|[12][0-9]|3[01]). It rejects 00 and anything above 31, which the loose \d{2} waved through.

The separator is a plain /. You type the pattern body without slashes in this tester, so it needs no escaping. (If your format uses dots, as in 25.12.2026, write the separator \., because a bare dot matches any character.)

The month piece mirrors the day piece with a shorter range: (0[1-9]|1[0-2]) gives 01 to 12 and rejects 00 and 13. The year piece is \d{4}, any four digits. Tightening it to this century, say (19|20)\d{2}, is a policy decision about your data, not a correctness fix, so make it deliberately.

Assembled: (0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\d{4}. Each group earns its place, and if the pattern misbehaves later you know which piece to interrogate.

The ISO pattern: same pieces, different order

The ISO format, YYYY-MM-DD, rearranges the same decisions: year first, then month, then day, hyphen separators. The pattern is \d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]). The month and day pieces are identical to the ones you just built, which is the point of the piece-by-piece method: the pieces are reusable, only the order and separator change. A hyphen needs no escaping here, so the pattern stays readable.

Run it against 2026-12-25 ok, 2026-13-01 bad, 2026-02-31 shape-ok and the tester reports two matches: 2026-12-25 and 2026-02-31. The month 13 is refused, exactly as designed. The February date is accepted, also as designed: the design only covers shape.

How to test a date pattern, step by step

  1. Open the Regex Tester and type your assembled pattern in the Pattern field, without slashes.
  2. Put g in the Flags field, so every match in your test string is listed, not just the first.
  3. Paste a test string mixing dates that should pass, dates that should fail, and at least one calendar impossibility.
  4. Read the match list: every expected pass appears, every shape failure is absent, and the calendar impossibility appears, which is correct behaviour.
  5. If a piece misbehaves, test it alone on a short string before reassembling the pattern.

Worked example: one test string, four verdicts

Take the DD/MM/YYYY pattern and this test string: Party on 25/12/2026, not 31/02/2026, not 12/25/2026, not 5/12/2026. The tester reports exactly two matches: 25/12/2026 and 31/02/2026. Each verdict teaches something.

25/12/2026 matches, as it should: day 25, month 12, both in range. 12/25/2026, the American ordering, fails, because the month piece refuses 25; the pattern enforces the format it was built for. 5/12/2026 fails because the day piece demands two digits; a one-digit day needs the looser variant from the FAQs, a deliberate trade of strictness for tolerance. And 31/02/2026 matches: both pieces are in range, and the pattern cannot know February is short. Two passes, two refusals, and one pass is a date that does not exist. That is the whole character of date regex in one line of output.

Extracting dates versus validating a field

The same pattern does two jobs depending on anchors. Unanchored with the g flag, it extracts: every date-shaped string inside longer text, what you want for logs and documents. Add ^ at the front and $ at the end, and it validates: the entire input must be one date and nothing else, what you want for a form field. An unanchored pattern used as a validator passes meet on 25/12/2026 please, because the date is in there somewhere. Decide the job first, then set the anchors.

Capturing the parts with groups

The pieces are already wrapped in groups, so every match in the list shows its day, month and year as separate capture groups. When downstream code needs the parts labelled rather than numbered, name them: (?<day>\d{2})/(?<month>\d{2})/(?<year>\d{4}) uses the loose pieces with names, and the named values appear in each match's details. It matters for dates, where group 1 is the day in one pattern and the year in another, a classic source of quiet bugs.

Limitations

The calendar limit is absolute: the pattern cannot know month lengths, so 31/02 and 31/04 pass, and leap years are entirely beyond it. Treat a match as a shape pass, and run a real date validation afterwards wherever correctness matters. The strict pieces demand two-digit days and months with leading zeros, so genuine dates written as 5/12/2026 are refused unless you loosen the pattern. The tester runs JavaScript's RegExp engine, whose syntax differs slightly from other flavors, and results stop at 500 matches, which matters when extracting from long documents.

Common mistakes

  • Copying a pattern without testing it. Run it against strings that should fail as well as pass; the failures are where the truth is.
  • Using an unescaped dot as a separator. A bare . matches any character, so 25x12x2026 passes. Write \. for a full-stop separator.
  • Validating with an unanchored pattern. Surrounding text sneaks through. Anchor with ^ and $ when the whole field must be a date.
  • Believing a match means a real date. 31/02/2026 matches. Calendar truth needs a date check after the pattern, every time.

FAQs

Can a regex check that a date is a real calendar date? No. A regex checks the shape of the string, with the day and month in range. It cannot know that February has no 31st, and leap years are beyond it. Use the pattern to filter, then validate the date in code.

Why does my date pattern miss dates like 5/12/2026? The strict day piece, (0[1-9]|[12][0-9]|3[01]), demands exactly two digits, so a one-digit day fails. If your input mixes both forms, loosen the day and month pieces to one or two digits, and accept that the pattern checks less.

Do I need to escape the slash in a date pattern? Not in this tester. You type the pattern body without slashes, so / is an ordinary character here. A dot is different: escape it as \. when the separator is a full stop, because a bare dot matches any character.

How do I pull dates out of a paragraph instead of validating a field? Leave the ^ and $ anchors off and set the g flag. The pattern then lists every date-shaped string in the text, instead of passing or failing the whole input.

Can I capture the day, month and year separately? Yes. Wrap each piece in a capture group and the match list shows the groups for every match. Named groups work too: (?<day>...) style names appear in each match's details.

Conclusion

A date pattern you built yourself holds no surprises: a day range, a separator, a month range and a year, each tested as it was added, in the Regex Tester. Keep the limit in view: shape is not calendar truth. When a pattern misbehaves and you cannot see why, the companion guide on why your regex is not matching works through the failure list in order. More guides live in the Developer Tools guides archive.