How to Use Regex Capture Groups for File Renaming
Capture groups are the most powerful feature of regex for file renaming. They let you extract specific portions of a filename and rearrange them into a completely new structure. Instead of just finding and removing text, capture groups enable you to restructure filenames by referencing matched portions in the replacement string.
How Do Capture Groups Work in Regex?
$1 for the first group,$2 for the second, and so on. Groups are numbered left to right by their opening parenthesis.Consider a filename like Smith_John.pdf where you want to produce John_Smith.pdf. The regex pattern ^([A-Za-z]+)_([A-Za-z]+) creates two capture groups. The first group ([A-Za-z]+)captures "Smith" and the second group captures "John". The replacement string $2_$1 outputs the groups in reversed order, producing John_Smith. The pattern matches any pair of alphabetic strings separated by an underscore.
Groups can capture any pattern, not just simple character sequences. A group containing (\d4-\d2-\d2) captures a date in YYYY-MM-DD format. A group containing (.*?) captures any text using a non greedy match. Combining multiple groups lets you decompose complex filenames into individual components and reassemble them in any order. For the fundamentals of regex pattern matching, see the regex rename guide.
How Do You Restructure Filenames with Capture Groups?
^(\d+)_([a-zA-Z]+)_(\d4) on the filename 001_report_2026 captures the number, name, and year. The replacement$3_$2_$1 produces 2026_report_001.Filename restructuring is the primary use case for capture groups. Many file collections arrive with naming conventions that do not match your organizational needs. Camera files use DSC_0001 format. Downloaded files have random ID prefixes. Exported spreadsheets contain date suffixes in non standard positions. Capture groups let you extract each meaningful component and place it where you want it.
The key to effective restructuring is identifying the separators and patterns in the original filenames. Underscores, hyphens, spaces, and dots commonly separate filename components. Fixed length numbers, known text strings, and date patterns provide anchors for your capture groups. Start by listing the components you want to extract, write a group for each one, and then construct the replacement string with the groups in your preferred order. For practical recipe examples, see the regex find and replace cookbook.
How Do Named Capture Groups Improve Readability?
(?<name>pattern) to assign a descriptive label instead of relying on numeric indices. The replacement references the group by name. Named groups make complex patterns self documenting and reduce errors when patterns contain many groups.Numbered groups become confusing when a pattern contains four or more captures. Remembering that $3 is the date and $4 is the version number requires mental tracking that leads to errors. Named groups solve this by letting you write(?<date>\d4-\d2-\d2) and reference it as $date in the replacement string. The pattern becomes a readable description of the filename structure.
Batchio's regex engine supports named capture groups through the Find & Replace rule. The live preview shows which named group captured which text for every file, making it easy to verify complex patterns before committing. Terminal's sed does not support named groups, but the Perl rename command does. Choose named groups whenever your pattern contains three or more captures. For basic regex concepts, review the regex rename guide.
How Do You Use Capture Groups in Terminal?
sed or the Perl renamecommand. In sed, groups use escaped parentheses \(pattern\) and backreferences use \1, \2. The Perl rename command uses standard parentheses and $1, $2 syntax, which is more readable and consistent with modern regex conventions.The sed command on macOS uses POSIX Basic Regular Expressions by default, which requires escaping parentheses with backslashes. The command echo "Smith_John" | sed 's/\([A-Za-z]*\)_\([A-Za-z]*\)/\2_\1/' swaps the two name components. Adding the -E flag switches to Extended Regular Expressions, which uses unescaped parentheses: sed -E 's/([A-Za-z]+)_([A-Za-z]+)/\2_\1/'.
The Perl rename command provides the cleanest syntax for capture group operations. The command rename 's/^(\d+)_(.+)/$2_$1/' *.txt moves the leading number to the end of every .txt filename. Always use the -n flag first to preview changes: rename -n 's/pattern/replacement/' *. For a comprehensive Terminal reference, see the batch rename guide.
How Does Batchio Handle Capture Groups?
$1, $2in the Replace field. The live preview highlights each group's match and shows the resulting filename for every file.Batchio's visual interface makes capture groups accessible without memorizing command line syntax. The Find field accepts any regex pattern with parenthesized groups. The Replace field accepts group references and literal text. As you type, the live preview updates to show which text each group captured and what the final filename looks like. This immediate feedback loop lets you iterate on complex patterns quickly.
Batchiosupports numbered groups ($1 through $99) and named groups for advanced patterns. The regex engine handles Unicode characters, case insensitive matching, and non greedy quantifiers. Stack the Find & Replace rule with other rule types to combine capture group restructuring with additional transformations like case changes or numbering.
Frequently Asked Questions
What is a capture group in regex?
What is the difference between $1 and \1 in regex replacement?
Can you use named capture groups for file renaming?
How many capture groups can you use in a single rename pattern?
Visual Capture Group Renaming
Download Batchio free on the Mac App Store. All 9 rule types included. Pro upgrade $4.99.
Coming Soon to the Mac App StoreMarcel Iseli is an indie app developer and the creator of Batchio. He builds native macOS utilities focused on productivity and file management, with a focus on lightweight, subscription-free tools.