Regex Tester · Tutorial
How to find and reformat dates with a regular expression
8 min readintermediateUpdated
Dates written as 03/11/2026 are ambiguous (is it March or November?) and sort badly. ISO 8601 dates like 2026-11-03 are unambiguous and sort correctly as plain text. This tutorial builds a regular expression that finds day/month/year dates in text, captures each part with a named group, and uses a replacement to rewrite them. You'll then be able to use the same pattern in your editor or code.
What you need
- The Regex Tester
- Some text with dates in DD/MM/YYYY format (the Try it button loads order notes)
Steps
Start with the digits
A date like
03/11/2026is two digits, a slash, two digits, a slash and four digits. In regex,\dmeans a digit and{2}means exactly two, so the shape is\d{2}/\d{2}/\d{4}. Type it into the pattern box with the g flag on. All four dates in the sample are highlighted, but2/3in the last line isn't, because it doesn't have the full shape.Name each part with a group
Wrap each part in a named group so you can reuse it:
(?<name>...). The full pattern becomes the one below. Open Matches & groups: each match now has three columns,day,monthandyear.(?<day>\d{2})/(?<month>\d{2})/(?<year>\d{4})Check the explanation
Open the Explanation tab. Each piece of the pattern is described in plain English, such as "start of a group named “day”" and "a digit (0–9), exactly 2 times". This is a good way to confirm the pattern means what you intended.
Write the replacement
Open the Replace tab and enter
$<year>-$<month>-$<day>. Each$<name>is replaced with that group's text. The preview shows the converted text:03/11/2026becomes2026-11-03, and the other dates are converted the same way.Order 1182 shipped 2026-11-03 and arrived 2026-11-07. Refund requested 2026-11-15; approved 2026-11-18.Use the pattern elsewhere
The same pattern works in JavaScript (
text.replace(/(?<day>\d{2})\/(?<month>\d{2})\/(?<year>\d{4})/g, '$<year>-$<month>-$<day>')), and with small changes in other languages. Python writes named groups as(?P<day>...)and uses\g<day>in replacements. You can also paste it into the Find and Replace tool with Regular expression ticked.
Try it yourself
Opens the Regex Tester with this tutorial's sample loaded.
Common mistakes
- Single-digit days and months
\d{2}won't match3/11/2026. Use\d{1,2}to accept both, but note that replacing will then give2026-11-3, which isn't valid ISO 8601. Pad single digits first, or in code.- Mixing up day/month order
- US-style dates are month/day/year. Check where your text comes from before deciding which group is the day and which the month.
- Matching inside longer numbers
- A pattern can match part of a longer string, such as within
123/45/67890. Add word boundaries,\b...\b, around the pattern if that's a risk.
Next steps
Go back to the Regex Tester, or keep going with one of these:
FAQ
Does the regex check that the date is real?
No. It checks the shape, so 31/02/2026 would still match. Validate the actual date in code after extracting it.