Regexp Cheatsheet
A quick reference for the regular expression syntax available in SmartRenamer’s Search and Replace rule (Regexp mode) and the Regexp Extract variable filter. All examples use filenames.
Character Classes
Character classes let you match specific kinds of characters.
| Pattern | Meaning | Example pattern | Example input | Match |
|---|---|---|---|---|
. | Any single character | r..ort | report.pdf | report |
\d | Any digit (0–9) | \d+ | IMG_1234.jpg | 1234 |
\D | Any non-digit | \D+ | IMG_1234.jpg | IMG_ |
\w | Any word character (letter, digit, _) | \w+ | my_file-2.txt | my_file |
\W | Any non-word character | \W | my_file-2.txt | - |
\s | Any whitespace (space, tab) | \s+ | my file.txt | (space) |
\S | Any non-whitespace | \S+ | my file.txt | my |
[abc] | Any one of a, b, or c | [aeiou] | report.pdf | e |
[^abc] | Any character except a, b, c | [^aeiou] | report.pdf | r |
[a-z] | Any character in range a to z | [A-Z]+ | IMG_1234.jpg | IMG |
[a-zA-Z0-9] | Letters and digits | [a-zA-Z]+ | file_99.txt | file |
Tips
- Inside
[...], most special characters lose their meaning—you don’t need to escape.or*there. - Use
[^...]to match anything except the listed characters:[^0-9]matches any non-digit.
Quantifiers
Quantifiers control how many times the preceding element must appear.
| Pattern | Meaning | Example pattern | Example input | Match |
|---|---|---|---|---|
* | 0 or more times | \d* | abc.txt | “ (empty — zero digits) |
+ | 1 or more times | \d+ | file_42.txt | 42 |
? | 0 or 1 time (optional) | _?file | myfile.txt | file |
{3} | Exactly 3 times | \d{3} | IMG_007.jpg | 007 |
{2,} | 2 or more times | \d{2,} | a1b23c456.txt | 23 (first match with 2+ digits) |
{2,4} | Between 2 and 4 times | \d{2,4} | file_12345.txt | 1234 |
Greedy vs. Lazy
By default, quantifiers are greedy — they match as much text as possible. Add ? after a quantifier to make it lazy (match as little as possible).
| Pattern | Input | Match |
|---|---|---|
.+_ (greedy) | a_b_c_file.txt | a_b_c_ |
.+?_ (lazy) | a_b_c_file.txt | a_ |
Anchors and Boundaries
Anchors match a position, not a character.
| Pattern | Meaning | Example pattern | Example input | Matches? |
|---|---|---|---|---|
^ | Start of the string | ^IMG | IMG_001.jpg | ✅ Yes |
^ | Start of the string | ^IMG | my_IMG_001.jpg | ❌ No |
$ | End of the string | final$ | report_final | ✅ Yes |
\b | Word boundary | \bfile\b | myfile.txt | ❌ No |
\b | Word boundary | \bfile\b | my file.txt | ✅ Yes |
\B | Non-word boundary | \Bfile | myfile.txt | ✅ Yes |
Tips
^and$are useful to ensure the pattern matches the full filename, not just part of it.\bis great for matching whole words without affecting surrounding text.
Groups and Capture
Groups let you treat multiple characters as a single unit, and capture groups let you extract or reuse matched parts.
| Pattern | Meaning | Example |
|---|---|---|
(...) | Capturing group — remembers the match | (\d{4})-(\d{2})-(\d{2}) captures year, month, day |
(?:...) | Non-capturing group — groups without remembering | (?:IMG|PHOTO)_\d+ matches either prefix |
(?<name>...) | Named capturing group | (?<year>\d{4})-(?<month>\d{2}) |
Capture Group Examples
Extract a date from a filename:
- Pattern:
(\d{4})-(\d{2})-(\d{2}) - Input:
report_2026-03-08_final.pdf - Group 1:
2026, Group 2:03, Group 3:08
Using groups in replacement text (Search and Replace):
- Pattern:
(\w+)_(\d+) - Replace with:
$2_$1 - Input:
photo_001.jpg→ Output:001_photo.jpg
$0 refers to the entire match, $1 to the first group, $2 to the second, and so on.
Using groups with the Regexp Extract filter (Variables):
- Variable:
{name | regexp: (\d{4})-(\d{2})-(\d{2}), 1} - Input filename:
report_2026-03-08.pdf - Result:
2026
Alternation
The pipe | acts as an “or” operator.
| Pattern | Meaning | Example input | Match |
|---|---|---|---|
jpg|png|gif | Match jpg, png, or gif | photo.png | png |
(IMG|PHOTO)_\d+ | Match IMG_ or PHOTO_ followed by digits | PHOTO_42.jpg | PHOTO_42 |
Tips
- Always use parentheses to limit the scope of
|. Without them,abc|defmeans “matchabcordef”, not “matchab, thencord, thenef”.
Lookahead and Lookbehind
Lookaround checks for a pattern without including it in the match. Useful to match text only if it appears before or after something specific.
| Pattern | Meaning | Example pattern | Example input | Match |
|---|---|---|---|---|
(?=...) | Lookahead — followed by… | \w+(?=\.pdf) | report.pdf | report |
(?!...) | Negative lookahead — NOT followed by… | \w+(?!\.pdf) | report.txt | report |
(?<=...) | Lookbehind — preceded by… | (?<=IMG_)\d+ | IMG_1234.jpg | 1234 |
(?<!...) | Negative lookbehind — NOT preceded by… | (?<!IMG_)\d+ | DSC_5678.jpg | 5678 |
Example: Match filename body without extension
- Pattern:
.+(?=\.\w+$) - Input:
my.long.report.pdf - Match:
my.long.report
Escaping Special Characters
These characters have special meaning in regexp: . * + ? ^ $ | \ ( ) [ ] { }.
To match them literally, prefix with a backslash \.
| To match | Use | Example pattern | Example input | Match |
|---|---|---|---|---|
. (literal dot) | \. | report\.pdf | report.pdf | report.pdf |
( and ) | \( and \) | \(\d+\) | file(1).txt | (1) |
[ and ] | \[ and \] | \[\d+\] | file[2].txt | [2] |
\ (literal backslash) | \\ | folder\\file | folder\file | folder\file |
Common Recipes
Here are practical regexp patterns for everyday file renaming tasks.
Remove a date
- Pattern:
\d{4}-\d{2}-\d{2}_? - Replace with: (empty)
report_2026-03-08_final.pdf→report_final.pdf
Swap two parts
- Pattern:
^(\w+)_(\w+)$ - Replace with:
$2_$1 first_second→second_first
Add zero-padding to numbers
- Pattern:
\b(\d)\b - Replace with:
00$1 file_1.txt→file_001.txt
Remove everything inside parentheses
- Pattern:
\s*\([^)]*\) - Replace with: (empty)
file (copy).txt→file.txt
Extract episode number
- Variable:
{name | regexp: [Ee](\d+)} - Input:
show_S02E14_title.mkv - Result:
14