English

Text & everyday tools · Text Toolkit

Regex capture groups in find and replace: reorder dates with $1, $2, $3

· How it works

regex find-and-replace dates

Three captured date fields moving from year-month-day into day-month-year order
Original ToolAcre vector illustration

Shows how parentheses capture parts of a match and how $1, $2 and $3 reuse them in the replacement, using the classic ISO-to-day-month-year date rewrite and a few other reorderings.

Four hundred dates in the wrong order — why hand-editing 2024-01-02 into 02/01/2024 is a job for capture groups

A spreadsheet export can leave four hundred dates in ISO-style year-month-day order when a UK worksheet expects day/month/year. Editing `2024-01-02` into `02/01/2024` by hand is slow, but the larger risk is inconsistency: one skipped row or transposed digit can survive until a report is distributed.

Find and replace can treat every date as the same arrangement of parts rather than as four hundred unrelated strings. A regular expression identifies the year, month and day, while the replacement writes those captured parts back in another order. The original digits are reused, so the operation changes structure rather than retyping values.

What parentheses capture — how each group gets a number from left to right, and what \d{4} and \d{2} match

Parentheses create capture groups. In `(\d{4})-(\d{2})-(\d{2})`, the first group captures four digits, the second captures two, and the third captures two. `\d` means a digit in this JavaScript pattern, while `{4}` and `{2}` specify the exact number of repetitions required inside each group.

Groups receive numbers from each opening parenthesis as the engine reads from left to right. For `2024-01-02`, group 1 is `2024`, group 2 is `01`, and group 3 is `02`; the hyphens match separators but sit outside the parentheses, so they are not retained as captured values.

Using $1, $2 and $3 in the replacement — rebuilding the match in a new order with new separators

The replacement `$3/$2/$1` asks JavaScript to insert the third capture, a slash, the second capture, another slash, and the first capture. Applied to the example, those references produce `02/01/2024`. Dollar references are replacement instructions, not text that must also appear in the matched date.

ToolAcre passes the replacement string to the standard JavaScript string replacement operation. Its source explicitly supports numbered references such as `$1`, `$2` and `$3`, and the search pattern is compiled globally. That global flag is why every matching date is rewritten in one operation instead of only the first match.

Worked example — (\d{4})-(\d{2})-(\d{2}) replaced with $3/$2/$1, then checking the replacement count against the row count

Paste the exported column, turn on Regex, enter `(\d{4})-(\d{2})-(\d{2})` in Find, and enter `$3/$2/$1` in Replace. Selecting Replace all first counts the matches and then rewrites them, so the result reports how many date-shaped strings were changed across the entire pasted text.

Compare that number with the expected row count before copying the result back to the spreadsheet. A lower count points to blanks or dates in another format; a higher count means similar text elsewhere also matched. The tool has no replace-one preview, so use its Undo action if the count or output reveals an over-broad pattern.

More reorderings — 'Surname, Forename' to 'Forename Surname', and swapping the columns of a two-column list

The same principle can change `Surname, Forename` into `Forename Surname` when each line reliably contains one comma: find `([^,]+), ([^\r\n]+)` and replace with `$2 $1`. Here each group captures text on one side of the known separator, rather than assuming a fixed number of digits.

A two-column swap is not a built-in table operation, despite the broad outline wording. It works only when the pasted text has a dependable delimiter and the pattern describes line boundaries safely. Commas inside names, missing fields, or inconsistent tabs require cleanup or a more precise pattern before any bulk replacement is trustworthy.

More reorderings work only when the input has a dependable separator

A dot in a pattern means almost any character, so a date pattern written as `(\d{4}).(\d{2}).(\d{2})` also accepts slashes, spaces or letters between fields. Escape a literal dot as `\.` and escape other metacharacters when they should match themselves. An invalid bracket or unfinished parenthesis is reported without changing the text.

Group numbering starts at 1, not 0. Dollar signs also have special replacement meaning: `$&` inserts the whole match and `$$` produces one literal dollar sign. This behavior applies even when regex mode is off because ToolAcre escapes only the search side; it does not escape the replacement string before JavaScript processes it.

What this does not cover — named groups, lookbehind and month names that need a lookup table

Numbered groups can rearrange text already present in a match, but they cannot translate `Jan` into `01` because that requires a lookup rather than reordering. This article also does not rely on named groups or lookbehind, whose usefulness and browser support involve a different set of choices than this simple export cleanup.

The date expression checks shape, not calendar truth. It will match `2024-99-88` because two digits satisfy each smaller group. Validate dates in the spreadsheet or source system when correctness matters. Find and replace is appropriate for consistent formatting, not for proving that each captured month and day is valid.

The takeaway — one pattern, one replacement, every row fixed at once in the Text Toolkit, with undo if the result is not what you expected

Capture groups turn one matched string into reusable pieces. For a uniform date column, three groups preserve the original year, month and day while `$3/$2/$1` places them in UK order. The global search handles all matches, and the reported count supplies an immediate check against the number of rows you intended to change.

Keep the workflow reviewable: retain a copy of the export, use the narrowest pattern that fits it, compare the count, inspect several results, and undo when the evidence disagrees with your expectation. One well-scoped replacement can remove repetitive editing without pretending that every digit-shaped value is automatically a valid date.