Search code examples
regexcapturereplacefile-rename

File Renamer Regular expression to Remove Repeated string


I Have FOLDER names like these examples

sons.of.anarchy.s05e01.hdtv.x264-2hd.nfo - sons.of.anarchy.s05e01.hdtv.x264-2hd.
sons.of.anarchy.s05e03.hdtv.x264-asap.nfo - sons.of.anarchy.s05e03.hdtv.x264-asap.
Jump.3.training.Or.Wand.3410.7200k.Blade.DD.5.1.x300-NTb - Jump.3.training.Or.Wand.3410.7200k.Blade.DD.5.1.x300-NTb.

And similar FILE names like these samples

sons.of.anarchy.s05e01.hdtv.x264-2hd.nfo - sons.of.anarchy.s05e01.hdtv.x264-2hd.mp4
Jump.3.training.Or.Wand.3410.7200k.Blade.DD.5.1.x300-NTb - Jump.3.training.Or.Wand.3410.7200k.Blade.DD.5.1.x300-NTb.avi

I want to build a RegEx that will rename the files/folders with the repeated string removed.

Folders Like this:

sons.of.anarchy.s05e01.hdtv.x264-2hd
sons.of.anarchy.s05e03.hdtv.x264-asap
Jump.3.training.Or.Wand.3410.7200k.Blade.DD.5.1.x300-NTb

Files like this

sons.of.anarchy.s05e01.hdtv.x264-2hd.mp4
Jump.3.training.Or.Wand.3410.7200k.Blade.DD.5.1.x300-NTb.avi

Ultimately I'm Aiming for the names to look like this:

Sons Of Anarchy S05E01
Sons Of Anarchy S05E03
Jump 3 Training Or Wand
Sons Of Anarchy S05E01.mp4
Jump 3 Training Or Wand.avi

I use custom Expressions to remove specific strings, periods & hyphens And it takes several steps. Here are a few expressions I use to clean names up in Renamer:

Find: [\.\-_/~\[\])("] and I replace with a space

Find: \s*$ and I replace with nothing to Remove spaces at the end of Names

Solution

  • You can do that using regex pattern

    ^(.*?)([.]\w*|)\s+[-]\s+\1([.](?=$))?([.]\w*|)$
    

    with replacement of

    $1$4
    

    See this Perl demo.


    To learn more about regular expressions, visit this site.