Skip to Content
User GuideRegexp Cheatsheet

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.

PatternMeaningExample patternExample inputMatch
.Any single characterr..ortreport.pdfreport
\dAny digit (0–9)\d+IMG_1234.jpg1234
\DAny non-digit\D+IMG_1234.jpgIMG_
\wAny word character (letter, digit, _)\w+my_file-2.txtmy_file
\WAny non-word character\Wmy_file-2.txt-
\sAny whitespace (space, tab)\s+my file.txt (space)
\SAny non-whitespace\S+my file.txtmy
[abc]Any one of a, b, or c[aeiou]report.pdfe
[^abc]Any character except a, b, c[^aeiou]report.pdfr
[a-z]Any character in range a to z[A-Z]+IMG_1234.jpgIMG
[a-zA-Z0-9]Letters and digits[a-zA-Z]+file_99.txtfile

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.

PatternMeaningExample patternExample inputMatch
*0 or more times\d*abc.txt“ (empty — zero digits)
+1 or more times\d+file_42.txt42
?0 or 1 time (optional)_?filemyfile.txtfile
{3}Exactly 3 times\d{3}IMG_007.jpg007
{2,}2 or more times\d{2,}a1b23c456.txt23 (first match with 2+ digits)
{2,4}Between 2 and 4 times\d{2,4}file_12345.txt1234

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).

PatternInputMatch
.+_ (greedy)a_b_c_file.txta_b_c_
.+?_ (lazy)a_b_c_file.txta_

Anchors and Boundaries

Anchors match a position, not a character.

PatternMeaningExample patternExample inputMatches?
^Start of the string^IMGIMG_001.jpg✅ Yes
^Start of the string^IMGmy_IMG_001.jpg❌ No
$End of the stringfinal$report_final✅ Yes
\bWord boundary\bfile\bmyfile.txt❌ No
\bWord boundary\bfile\bmy file.txt✅ Yes
\BNon-word boundary\Bfilemyfile.txt✅ Yes

Tips

  • ^ and $ are useful to ensure the pattern matches the full filename, not just part of it.
  • \b is 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.

PatternMeaningExample
(...)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.

PatternMeaningExample inputMatch
jpg|png|gifMatch jpg, png, or gifphoto.pngpng
(IMG|PHOTO)_\d+Match IMG_ or PHOTO_ followed by digitsPHOTO_42.jpgPHOTO_42

Tips

  • Always use parentheses to limit the scope of |. Without them, abc|def means “match abc or def”, not “match ab, then c or d, then ef”.

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.

PatternMeaningExample patternExample inputMatch
(?=...)Lookahead — followed by…\w+(?=\.pdf)report.pdfreport
(?!...)Negative lookahead — NOT followed by…\w+(?!\.pdf)report.txtreport
(?<=...)Lookbehind — preceded by…(?<=IMG_)\d+IMG_1234.jpg1234
(?<!...)Negative lookbehind — NOT preceded by…(?<!IMG_)\d+DSC_5678.jpg5678

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 matchUseExample patternExample inputMatch
. (literal dot)\.report\.pdfreport.pdfreport.pdf
( and )\( and \)\(\d+\)file(1).txt(1)
[ and ]\[ and \]\[\d+\]file[2].txt[2]
\ (literal backslash)\\folder\\filefolder\filefolder\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
Last updated on