Regex Find and Replace for Filenames on Mac
Regex find and replace transforms messy filenames into clean, consistent names using pattern matching. This guide provides practical recipes for the most common filename cleanup tasks. Each recipe includes the find pattern, the replacement string, and an explanation of how the pattern works.
How Do You Remove Numbers from Filenames with Regex?
\d+ matches one or more consecutive digits. Replace with an empty string to remove all numbers. The pattern ^\d+[_\s]* targets only leading numbers and their trailing separators. The pattern [_\s]*\d+$targets only trailing numbers with their leading separators.Removing leading numbers is the most requested regex recipe for file renaming. Files downloaded from content management systems, exported from databases, or scraped from websites often carry numeric prefixes like 001_, 1234_, or 20260326_. The pattern ^\d+[_\-\s]* matches one or more digits at the start of the filename followed by optional underscores, hyphens, or spaces. Replace with an empty string to strip the prefix completely.
Removing all numbers from a filename is more aggressive and uses the global flag: \d+ with replace all enabled. This strips every numeric sequence from the filename, which is useful for removing version numbers, IDs, and counters simultaneously. Be cautious with this approach because it also removes meaningful numbers like dates or dimensions. For selective number removal, use anchored patterns that target specific positions. For the basics of regex patterns, see the regex rename guide.
How Do You Extract and Reformat Dates in Filenames?
(\d4)(\d2)(\d2) captures a compact date like 20260326 into three groups: year, month, and day. The replacement $1-$2-$3 produces the ISO format 2026-03-26. This recipe converts compact dates into readable formats without changing other filename components.Date reformatting is a common need when files arrive with dates in non standard formats. Camera exports often use YYYYMMDD_HHMMSS in the filename. Database exports may use MMDDYYYY or DDMMYYYY. The key is identifying the date pattern and writing groups around each component. For YYYYMMDD, the pattern (\d4)(\d2)(\d2) captures year ($1), month ($2), and day ($3) as separate groups.
Rearranging the groups in the replacement string converts between any date format. To go from MMDDYYYY to YYYY-MM-DD, use the pattern (\d2)(\d2)(\d4) and replace with $3-$1-$2. For dates with separators, include the separators in the pattern: (\d2)/(\d2)/(\d4) matches dates with slashes. See the capture groups guide for advanced group techniques and the batch rename guide for the complete workflow.
How Do You Swap Name Parts in Filenames?
^([^_]+)_([^_]+) captures two components separated by an underscore. The replacement $2_$1 outputs them in reversed order. This recipe converts LastName_FirstName.pdf to FirstName_LastName.pdf and works with any two component filename structure.Name swapping uses two capture groups that divide the filename at a known separator. The character class [^_]+ matches one or more characters that are not underscores, which effectively captures everything up to the first underscore. The second group captures everything after the underscore. Reversing the groups in the replacement string swaps the two components.
For filenames with more than two components, add more groups. The pattern ^([^_]+)_([^_]+)_([^_]+) captures three underscore separated parts. The replacement $3_$1_$2 moves the third part to the front. Adjust the separator character in the pattern to match your filenames: use [^-]+ for hyphen separated names or [^ ]+ for space separated names. For complete capture group reference, see the capture groups guide.
How Do You Clean Up Special Characters in Filenames?
[^a-zA-Z0-9._\-] matches any character that is not a letter, number, dot, underscore, or hyphen. Replace with an empty string or an underscore to produce clean, URL safe filenames. The global flag ensures every special character in the filename is replaced, not just the first occurrence.Special characters in filenames cause problems with web servers, build tools, and cross platform file sharing. Parentheses, brackets, ampersands, and spaces can break command line scripts and URL paths. The negated character class [^a-zA-Z0-9._\-]defines a whitelist of allowed characters and matches everything else. Replacing matches with underscores preserves word separation while eliminating problematic characters.
Multiple consecutive spaces or special characters can produce double underscores after replacement. Add a second pass with the pattern _+ replacing with a single underscore to collapse duplicate separators. In Batchio, stack two Find & Replace rules: the first strips special characters and the second collapses duplicate underscores. The rules execute sequentially, producing clean results in one operation. See the Find & Replace feature for the complete interface reference.
How Do You Use Regex Find and Replace in Batchio?
Batchio validates regex patterns in real time and displays syntax errors as you type. The live preview uses color coding to show which portions of each filename match the pattern and what the resulting filename looks like after replacement. This visual feedback eliminates guesswork and lets you refine patterns incrementally. Toggle case sensitivity on or off to control whether the pattern matches uppercase and lowercase characters identically.
Stack multiple Find & Replace rules for multi step transformations. Each rule in the chain operates on the output of the previous rule, so you can decompose complex operations into simple, testable steps. The first rule might strip numbers, the second might normalize separators, and the third might apply a capture group restructuring. Drag rules to reorder them or disable individual rules to test combinations. For the basic regex concepts, see the plain text find and replace guide for simpler substitution workflows.
Frequently Asked Questions
Can you use regex find and replace in Finder?
How do you test regex patterns before renaming files?
What does the g flag do in regex find and replace?
Can regex find and replace handle Unicode filenames?
Regex Find and Replace with Live Preview
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.