Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

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.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have dates written like 12-31-2025 and want to change them to 2025/12/31. How do I match just that consistent format?

Geez

Start by describing the exact shape: two digits, a hyphen, two digits, another hyphen, then four digits. In regex, write that as ([0-9]{2})-([0-9]{2})-([0-9]{4}). Each parenthesized group captures one part: the first two digits, the next two, and the final four. [0-9] specifically means ASCII digits; Python’s \d can also match other Unicode decimal digits, so [0-9] is the clearer choice when you want only 0 through 9. [S1]

This pattern matches the stated shape, not a calendar date. It could match digit groups that do not correspond to a real date. Decide whether you want to find that text anywhere inside a larger string or require a whole-field match; those are different matching tasks.

Follow-up question

How do I use the captures to rearrange the date in Python?

Geez

Use re.sub with a replacement that places the captured year first, then the first and second two-digit groups:

import re

text = "Meeting: 12-31-2025"
pattern = r"([0-9]{2})-([0-9]{2})-([0-9]{4})"
updated = re.sub(pattern, r"\g<3>/\g<1>/\g<2>", text)
print(updated)

The output is Meeting: 2025/12/31. In the replacement, \g<1> refers to the first captured part, \g<2> to the second, and \g<3> to the third. Python documents this capture-reference form for substitution. [S1] If your input may contain other digit strings in the same shape, inspect the output before using it.

Follow-up question

What would the equivalent replacement look like in JavaScript, and how can I avoid changing unintended text?

Geez

For a string containing one occurrence, pass a regular expression with capture groups to replace and use $1, $2, and $3 in the replacement string:

const text = "Meeting: 12-31-2025";
const pattern = /([0-9]{2})-([0-9]{2})-([0-9]{4})/;
const updated = text.replace(pattern, "$3/$1/$2");
console.log(updated);

That produces Meeting: 2025/12/31. JavaScript replacement strings use numbered capture references when replace receives a regular expression. [S2] The example pattern can match a date-shaped substring inside longer text; it does not establish that the parts are a valid date or that a match is intended. Try the change on a copy and review the result.

Follow-up question

How do I check that the result is safe to use as a date?

Geez

Treat matching and date validation as separate steps. This regex confirms only the widths and hyphen positions of the text components; it does not prove that they form a real calendar date. [S1][S2] If your task requires valid dates, use an appropriate date-parsing or validation step for your programming environment, and decide how it should handle invalid input before replacing anything. These pattern and replacement examples do not specify those validation rules.

A small review checklist helps prevent a successful text replacement from being mistaken for a correct date conversion: confirm the input convention, test representative examples, and inspect the changed output.

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]

Do the captures validate that the components form a real date?

No. The pattern checks text shape only, not whether the components form a real calendar date. Use a separate date-validation step if that distinction matters. [S1][S2]

Sources

References used for this guide.

  1. [S1] Python 3.15.2 re — Regular expression operations
    docs.python.org · Checked
  2. [S2] JavaScript String.prototype.replace()
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work