Bulk Replace
Regex

How to Use Capture Groups in a Regex Find and Replace

Learn regex capture groups for find and replace: reorder text, keep part of a match, use named groups and avoid the $10 trap. Tested, copy-ready examples.

Updated 5 minute read The Bulk Replace team

A capture group is any part of a regex pattern wrapped in parentheses. In a find and replace, each group remembers the text it matched, and you put that text back in the replacement with $1, $2 and so on (some tools use \1). That lets you keep part of a match, reorder pieces, or wrap text in new markup, all across thousands of lines at once. For example, find (\w+), (\w+) and replace with $2 $1 to turn Smith, Anna into Anna Smith.

Below are the rules for numbering groups, worked examples you can copy, named groups, and the small syntax traps that make a replacement come out wrong.

How groups are numbered

Groups are numbered by the position of their opening parenthesis, counting from the left, starting at 1. Group 0 (or $&) is the whole match.

Pattern: ((\d{4})-(\d{2}))-(\d{2}) against 2025-12-31

ReferenceOpening parenthesisCaptures
$0 / $&Whole match2025-12-31
$1First (2025-12
$2Second (2025
$3Third (12
$4Fourth (31

Nested groups count too, which is why it pays to count parentheses carefully on complex patterns.

Worked examples

1. Reorder a date

Find \b(\d{2})/(\d{2})/(\d{4})\b, replace with $3-$2-$1.

31/12/2025 becomes 2025-12-31. The \b at each end stops it matching inside longer numbers. Our guide to changing date formats with regex covers US dates and single-digit days.

2. Swap "Last, First" to "First Last"

Find ^(\w+), (\w+)(?=\r?$) (multiline mode on), replace with $2 $1.

The anchors make sure the whole line is a name pair, so other comma-separated text is left alone. The (?=\r?$) look-ahead allows for Windows line endings: in .NET-style engines $ matches only before \n, so a plain $ would fail when a hidden \r sits at the end of each line.

3. Keep the value, change the wrapper

Find <b>(.*?)</b>, replace with <strong>$1</strong>.

The lazy .*? stops at the first closing tag, so two bold words on the same line are converted separately rather than merged.

4. Add a prefix to a matched value

Find src="images/([^"]+)", replace with src="/assets/images/$1".

[^"]+ means "one or more characters that are not a quote", which captures the file name and stops at the closing quote.

5. Update a version number while keeping the label

Find (version\s*=\s*")[\d.]+("), replace with ${1}2.4.0$2.

The braces in ${1} matter here, because $12 would be read as group 12. More on that below.

6. Use the whole match

Find \b[A-Z]{2,}\b, replace with <abbr>$&</abbr>.

With match case turned on, every run of two or more capital letters, such as HTML or CSV, is wrapped in an abbr tag without needing a group at all. Without match case, the pattern would also catch ordinary words.

Named groups

On long patterns, numbers get confusing. Named groups label each part:

Find (?<day>\d{2})/(?<month>\d{2})/(?<year>\d{4})

Engine / toolReplacement syntax
.NET, PowerShell${year}-${month}-${day}
JavaScript$<year>-$<month>-$<day>
Python re.sub\g<year>-\g<month>-\g<day>

Named groups are also numbered, so $1 still works alongside the names in most engines.

Non-capturing groups

Sometimes you need parentheses for grouping but do not need the text later, for example to apply ? to several characters or to hold alternatives. Use (?:...):

Find (\w+)\.(?:jpe?g|png), replace with $1.webp.

The extension alternatives are grouped but not numbered, so the file name stays $1. This keeps your numbering stable when you add or remove optional parts.

Five traps that break replacements

1. $1 followed by a digit

In .NET and PowerShell, $10 means group 10 if that group exists. To write group 1 followed by a zero, use ${1}0. JavaScript behaves similarly. Using braces whenever a digit follows a group is a safe habit.

2. PowerShell eats the dollar sign

In PowerShell, a replacement in double quotes, such as "$1", is expanded as a PowerShell variable first and becomes empty. Always use single quotes: $t -replace '(\w+)@old\.com','$1@new.com'.

3. A literal dollar sign

To output a real $ in .NET or JavaScript replacements, write $$. So $$$1 outputs a dollar sign followed by group 1, which is handy for prices.

4. $1 or \1?

Most Windows tools, including VS Code and .NET-based tools, use $1 in the replacement. Notepad++ accepts both $1 and \1. Inside the pattern itself, a back-reference to an earlier group is written \1 in nearly every engine: \b(\w+) \1\b finds doubled words like the the. Our reference on $1 vs \1 in replacements lists each tool.

5. An optional group that did not match

If a group is optional, such as (www\.)?, and did not take part in the match, $1 inserts nothing. That is usually what you want, but check the preview for lines where the group was missing.

Test a pattern before you trust it

  1. Collect real samples. Copy five or six lines from your files, including awkward ones: the longest, the shortest, ones with extra spaces and ones that should not match.
  2. Run the search only. Check the match count and look at what each match covers. A match that runs further than expected usually means a greedy .*.
  3. Check each group. Most regex testers and many editors show what each numbered group captured. Make sure $1 is really what you think it is.
  4. Replace on a copy. Apply the replacement to a test folder, then compare before and after.

When testing on a website, set the regex flavor to match your tool. PowerShell and other .NET tools use the .NET flavor, VS Code uses JavaScript, and Notepad++ uses Boost, which is close to PCRE.

Applying capture-group replacements across many files

A capture-group pattern that works on one line can still surprise you on line 4,000 of file 300. Test the pattern on a sample, then look at every result before saving. In Bulk Replace, any rule can be switched to regex with capture groups, back-references, anchors, character classes and look-arounds, and the replacement uses $1-style references, for example $3-$2-$1 for the date example above. The before/after preview shows each rewritten match in every file, so you can untick odd cases before anything is written, and Bulk Replace backs up each run so it can be undone in one click.

If your pattern does not match at all, our troubleshooting guide for regex replace not working covers escaping, multiline mode and greedy quantifiers.

Frequently asked questions

How do I refer to a capture group in the replacement?

Most Windows tools, including .NET, PowerShell and VS Code, use $1, $2 and so on. Some editors also accept \1. Check your tool's documentation if $1 appears literally.

How do I put a number straight after a group in the replacement?

Use braces: ${1}0 means group 1 followed by a zero, while $10 would be read as group 10 in .NET-style engines when that group exists.

What is a non-capturing group?

(?:...) groups part of a pattern without saving it, so it does not get a number. Use it for alternatives like (?:jpg|png) when you do not need the text in the replacement.

Get three guides like this every month, plus software update notices.Subscribe →