Can unrar Extract Files with Regular Expressions?
The command-line utility unrar does not natively support
regular expressions for filtering and extracting files, relying solely
on basic wildcard matching. However, you can achieve regular expression
filtering by pairing unrar with shell utilities such as
grep and xargs on Unix-based systems or
PowerShell on Windows. This article explains the limitations of native
unrar pattern matching and provides direct commands to
extract files matching complex regex patterns.
Native Pattern Matching Limitations
When using native extraction commands such as unrar e
(extract to current directory) or unrar x (extract with
full paths), the utility only recognizes standard shell-style
wildcards:
*matches any sequence of characters.?matches any single character.
Syntaxes such as character classes ([a-z]), quantifiers
(+, {1,3}), alternations
(foo|bar), and anchors (^, $) are
not interpreted as regular expressions by unrar.
How to Extract Files Using Regex on Linux and macOS
To extract files matching a regular expression, list the archive
contents using bare format, filter the file list using
grep -E (extended regular expressions), and pass the
results back to unrar.
Extracting Without Folder Structure
To extract matching files directly into the current working directory, run:
unrar lb archive.rar | grep -E 'pattern' | xargs -d '\n' -I {} unrar e archive.rar "{}"Extracting With Full Directory Hierarchy
To preserve the archive's original directory structure, use the
x switch:
unrar lb archive.rar | grep -E 'pattern' | xargs -d '\n' -I {} unrar x archive.rar "{}"Breakdown of the Command
unrar lb archive.rar: Outputs a bare list of file paths inside the archive, excluding headers, footers, and technical metadata.grep -E 'pattern': Filters the output against your regular expression. For example, to match all PNG and JPG images ending in numbers, use'[0-9]+\.(png|jpe?g)$'.xargs -d '\n' -I {}: Reads each matching line separately, properly handling filenames with spaces, and passes each file as an argument into the extraction command.
How to Extract Files Using Regex in Windows PowerShell
On Windows, you can achieve the same behavior using PowerShell's built-in regex filtering:
& unrar lb archive.rar | Where-Object { $_ -match '^regex_pattern$' } | ForEach-Object { & unrar x archive.rar $_ }This pipes the bare file list directly into the
Where-Object cmdlet, which evaluates the
-match regular expression operator, then calls
unrar x for each matching path.