Regex replacement: rearrange text with capture groups
Swap year, month and day without retyping them—and see why a matching date can still be invalid.
To change 2026-10-15 into 15/10/2026, capture the year, month and day separately, then put those captures in a different order. You do not need to type a new replacement for every date.
This example changes the shape of text. It does not check whether each date exists. That distinction matters when you use the result in a spreadsheet or publish it.
Start with a small sample, including a bad date
Open the Regex playground and paste this into Test text:
Launch: 2026-10-15
Review: 2026-11-02
Invalid: 2026-99-99
Short: 2026-1-5
In Regular expression, enter this pattern without slash delimiters:
\b(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})\b
The three named groups capture four digits, two digits and two digits. The word boundaries stop it from matching inside a longer run of word characters, but they do not make it a calendar validator.
Under Options, keep Find all matches enabled. That is the g flag. You should see three matches; the one-digit month and day in the last line do not fit the pattern.
Reorder the captures in the replacement
Choose Replace and enter the following in Replace with. On a phone, first open Results to reach the replacement view.
$<day>/$<month>/$<year>
The replacement preview should read:
Launch: 15/10/2026
Review: 02/11/2026
Invalid: 99/99/2026
Short: 2026-1-5
The invalid date changed too. A pattern that only counts digits cannot know the number of days in a month. Use a date parser when calendar validity matters; use this replacement when you already know the input dates are valid and only need a different display format.
Disabling Find all matches changes only the first date. The tool's match count then drops from three to one. If a replacement seems to stop after the first occurrence, check that flag before rewriting the pattern.
Numbered groups work too
For a shorter pattern, remove the names:
\b(\d{4})-(\d{2})-(\d{2})\b
Then use this replacement:
$3/$2/$1
It produces the same output for this sample. Group numbers follow the opening capture parentheses, from left to right. Named groups are easier to maintain if you expect to edit the pattern later.
The playground uses JavaScript replacement syntax. In the replacement field, $1 inserts the first capture, $<year> inserts the named capture, $& inserts the whole match and $$ inserts a literal dollar sign. MDN's replacement reference lists these forms. Other editors and regex engines may use different replacement notation.
Keep the source and inspect the exceptions
Download the sample input, all-matches output and first-match-only output to compare them. The settings file preserves the pattern and replacement; doubled backslashes in that JSON are string escaping, not extra backslashes to paste into the pattern field.
On 15 October 2026, we ran these examples through the same evaluation function as the playground. Both capture styles produced identical output; switching off the global flag changed one match, and the invalid date still matched. The results record includes those checks and the files' hashes.
For a real draft, keep an untouched copy and include some lines that should not change in your test. A plausible-looking preview is not evidence that every replacement is correct.