Reformatting a thousand lines by hand is nobody's idea of an afternoon. Dates in the wrong order, names written first-name-first when the spreadsheet wants them last-name-first: these are jobs for a regex replacement, where capture groups pull a match apart and reassemble it in a new order. The risky part is that a replacement runs everywhere at once, on text you may not be able to undo. This guide shows a safer order of work: design the pattern, verify in the free Regex Tester what each group captures, and only then run the replacement where replacements actually run.
Contents
- Quick answer
- Capture groups, in plain terms
- Why verify before you replace
- How to verify and replace, step by step
- Worked example: swapping first and last names
- Worked example: reformatting dates in Find and Replace
- Limitations and alternatives
- Common mistakes
- FAQs
- Conclusion
Quick answer
Build your pattern with parentheses around the pieces you want to reuse, then paste it into the Regex Tester and read the match list: for every match it shows the groups, which are exactly what $1, $2 and so on will contain. The tester itself has no replace box, and this guide will not pretend otherwise. Run the replacement in your code, or in Find and Replace with regular expression mode on, where the replacement box supports $1 and $2 for groups and $& for the whole match.
Capture groups, in plain terms
A capture group is a part of a pattern wrapped in parentheses. When the pattern matches, the engine remembers what each pair of parentheses matched, numbering the groups from left to right by their opening parenthesis. A replacement string can then refer to those memories: $1 means "whatever group 1 captured", $2 means group 2, and $& means the entire match.
The tester makes this concrete. Its own example runs (\w+)@(\w+)\.com against [email protected] and shows groups ada and example. Building a replacement for email addresses, you would already know that $1 holds ada and $2 holds example, before a single character of real text is rewritten.
Why verify before you replace
A find operation that goes wrong costs you a wrong list. A replace operation that goes wrong costs you your text, rewritten wrongly everywhere the pattern touched. The failure modes are quiet ones: a group capturing one character more than expected, a pattern matching a line it should have left alone, a group order remembered backwards. All three are visible in the tester's match list, where each match shows its text, index and groups.
How to verify and replace, step by step
- Write the pattern with parentheses around each piece you plan to reuse, in the order your replacement will refer to them.
- Open the Regex Tester. Type the pattern without slashes in the Pattern field, and put any flags in the Flags field. Add the
gflag to see every match; without it, only the first match is reported. - Paste in sample text with several typical cases and at least one line that should not match. Read the match list line by line: check each match's text and index, then read its groups and say what
$1and$2would insert. - If a group holds the wrong piece, fix the pattern and retest. An invalid pattern shows a plain-language error; common causes are an unclosed bracket or parenthesis, a trailing backslash, or a quantifier with nothing to repeat.
- Run the replacement where it belongs. In code, use your language's regex replace with the verified pattern. For pasted text, open Find and Replace, turn on the Regular expression option, enter the same pattern and a replacement using
$1,$2or$&, and read the match count it reports before copying the result.
One more tester feature helps with complicated patterns: named groups are supported, written (?<name>...), and the named values appear in each match's details, which makes a long pattern easier to audit.
Worked example: swapping first and last names
Suppose a list reads Ada Lovelace, Grace Hopper, Alan Turing, one name per line, and you need Lovelace, Ada and so on. The pattern is (\w+) (\w+): group 1 takes the first word, a literal space, group 2 takes the second. The replacement is $2, $1.
Verify first. In the tester, with flag g, the match on Ada Lovelace shows group 1 as Ada and group 2 as Lovelace. The other lines report the same shape, and nothing else matches, so the pattern is safe to fire. The replacement is then a formality: in Find and Replace with regex mode on, $2, $1 turns the list into Lovelace, Ada, Hopper, Grace, Turing, Alan, and the tool reports 3 replacements made.
Note what verification bought: had a line read Ada King Lovelace, the tester would have shown the match covering only part of it, and you would have caught that first.
Worked example: reformatting dates in Find and Replace
Here is a date reformat end to end, a job the Find and Replace entry names itself. Text holds dates like 2026-11-27 and you need 27/11/2026. The pattern is (\d{4})-(\d{2})-(\d{2}): group 1 the year, group 2 the month, group 3 the day. The replacement is $3/$2/$1.
Verify in the tester first: the match on 2026-11-27 shows groups 2026, 11 and 27, in that order. Then run it in Find and Replace with the Regular expression option on. Read the match count, copy the result, and spot-check a couple of lines against the originals.
Limitations and alternatives
The division of labour in this guide is a real limitation: the Regex Tester cannot show you the replaced output, so your preview of a replacement is the group list, read carefully, plus the match count in Find and Replace. For a high-stakes replacement on a large file, work on a copy and keep the original until you have checked the result.
Patterns also carry the tester's own caveats. It runs JavaScript's RegExp engine, whose syntax differs slightly from PCRE, Python or grep, so a verified pattern still deserves a small test in its final home. Results stop at 500 matches, and a pathological pattern with nested quantifiers on long text can make your browser tab busy; simplify it if that happens. In Find and Replace, an empty Replace box deletes matches: a feature when intended, a nasty surprise when not.
For a plain word swap, skip regex entirely: Find and Replace in its default mode rewrites every occurrence and reports the count, case-insensitively by default. Regex earns its complexity only when you are looking for a shape, not a literal string.
Common mistakes
- Expecting a replace box in the tester. There is none. The tester verifies; code or Find and Replace replaces.
- Getting the group order wrong. Groups are numbered by their opening parenthesis, left to right, including nested groups. Read the order off the match list instead of counting parentheses from memory.
- Replacing without testing a non-matching line. A pattern that matches too much does its damage everywhere at once. One line that should stay untouched belongs in every test sample.
- Leaving regex mode off in Find and Replace. With the option off, the pattern is literal text and
$1lands in your output as the characters dollar and one. - Forgetting the g flag while verifying. Without it the tester reports only the first match, so a pattern that misbehaves on the fifth line looks perfect.
FAQs
Does the Regex Tester have a replace box? No. The tester lists matches with their positions and capture groups; it does not rewrite text. Use it to prove the pattern and its groups are right, then run the replacement in your code or in the Find and Replace tool with regular expression mode switched on.
What does $& mean in a replacement? In the Find and Replace tool's regex mode, $& stands for the whole match, while $1 and $2 refer to the first and second capture groups. A replacement like [$&] wraps each match in brackets without needing any groups at all.
How do I know what $1 will contain before I replace? Test the pattern in the Regex Tester first. Each match in its list shows the capture groups separately, so you can read group 1 and group 2 for every match and confirm they hold the pieces your replacement expects, in the order it expects them.
What happens if my regex is invalid in Find and Replace? An invalid regular expression shows a clear error and changes nothing, rather than failing silently or replacing halfway. Fix the pattern, ideally by testing it in the Regex Tester until it parses and matches as intended, then try the replacement again.
Why did my replacement insert the literal text $1 instead of the group? Because regular expression mode was off, so the tool treated both the pattern and the replacement as plain text. Turn on the Regular expression option and run it again; the match count will also confirm whether the pattern is matching at all.
Conclusion
Capture groups turn a regex from a finder into a rebuilder, and the safe way to use them is to separate proving from firing. Prove the pattern in the Regex Tester, where every group is on display, then fire the replacement in code or in Find and Replace with regex mode on. For a worked case of building a pattern piece by piece, read Regex for IP Addresses: IPv4 Pattern Explained. More tools for this kind of work are in the Developer Tools archive.