How can I find and replace dates written in one consistent text format?
Quick answer
Write a pattern for the format you actually have, such as ([0-9]{2})-([0-9]{2})-([0-9]{4}) for two digits, a hyphen, two digits, a hyphen, and four digits. The parentheses capture each part so a replacement can reorder them. This checks the characters’ shape—not whether they make a real date. Test on a copy before changing important text.
The question, the details, the next step.
A practical checklist
Write down the exact input shape, including digit counts and separator characters.
Use capture groups for the components you need to rearrange.
Confirm whether the pattern should match within text or only an entire field.
Test on a copy with representative strings and inspect the changed result.
Validate calendar dates separately if real-date validity matters.
A few more useful answers
What does `[0-9]{2}` match?
It matches exactly two ASCII digits in a row. The full pattern also requires the specified hyphens and a four-digit final component. [S1]
Sources
References used for this guide.
- [S1] Python 3.15.2 re — Regular expression operations docs.python.org · Checked
- [S2] JavaScript String.prototype.replace() developer.mozilla.org · Checked
