Back to Blog
Advanced2024-11-288 min read
How to Rename Files with Regex: Patterns for Power Users
How to Rename Files with Regex: Patterns for Power Users
Regular expressions (regex) enable powerful pattern-based renaming. Here's how to use them.
What is Regex?
Regex is a pattern-matching language:
Pattern: IMG_[0-9]+\.jpg
Matches: IMG_001.jpg, IMG_5847.jpg, IMG_99999.jpgCommon Regex Patterns for File Renaming
Match Any Number
[0-9]+
Matches: 1, 42, 5847, 123456Match Any Word Characters
\w+
Matches: photo, IMG, document2024Match File Extension
\.[a-z]+$
Matches: .jpg, .pdf, .pngCapture Groups
(IMG)_([0-9]+)\.jpg
Captures: IMG, 5847 (separately)Practical Regex Examples
Remove Numbers from Filenames
Pattern: [0-9]
Replace: (nothing)
Before: photo123.jpg
After: photo.jpgReplace Underscores with Hyphens
Pattern: _
Replace: -
Before: my_file_name.pdf
After: my-file-name.pdfRemove Camera Prefix
Pattern: ^(IMG|DSC|DSCN|DSCF)_
Replace: (nothing)
Before: IMG_5847.jpg
After: 5847.jpgAdd Date Prefix
Pattern: ^
Replace: 2025-01-06-
Before: report.pdf
After: 2025-01-06-report.pdfExtract and Reorder
Pattern: (\w+)-(\d+)\.jpg
Replace: \$2-\$1.jpg
Before: beach-001.jpg
After: 001-beach.jpgTools That Support Regex
Windows - Bulk Rename Utility - Advanced Renamer - PowerShell
Mac - A Better Finder Rename - Name Mangler - Terminal
Command Line
bash
# Bash example
for f in *.jpg; do
mv "$f" "$(echo $f | sed 's/IMG_/photo-/')"
doneWhen NOT to Use Regex
Regex is overkill when: - You just need descriptive names - Pattern varies too much - You're not comfortable with syntax
The Alternative: AI Renaming
Instead of writing patterns, let AI understand your files:
Regex approach:
Pattern: IMG_[0-9]+\.jpg → photo-\$1.jpg
Result: photo-5847.jpg (still meaningless)
AI approach:
Input: IMG_5847.jpg
Output: sunset-beach-hawaii.jpg (actually descriptive)File Renamer AI gives you the power of pattern matching plus the intelligence to create meaningful names.
Try File Renamer AI - Power without complexity.