#1 2014-09-04 22:49

Shabono
Member
Registered: 2014-09-04
Posts: 3

Looking to remove 2nd bit of text in square brackets in file names

I'm trying to clean up the file names in a large collection of files. I need to remove the second piece of text in square brackets in each file name. The RegEx  \[.*?\]  works to remove all of the text in square brackets but I need to remove just the second instance, ie:

"File_Name - [Blah00!] - blah [ABC-123].ext" needs to become:
"File_Name - [Blah00!] - blah .ext

Can anyone help please?

Offline

#2 2014-09-05 07:35

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Looking to remove 2nd bit of text in square brackets in file names

Hi and welcome, Shabono!

Again a great question from our users.

(And a well composed one with good before/after examples also, well done)


FROM:
File_Name - [Blah00!] - blah [ABC-123].ext

TO:
File_Name - [Blah00!] - blah.ext

Rule:
- match everything greedy untill last "space-openBracket"
- save match for later reuse into (...) backreference groups
- replace with what was matched into first backreference groups by $1
Therefore we match all parts of the whole file name to get the one we want.

USE:
RegEx
Expre: ^(.+)\s\[.+$
Repla: $1
[x] Skip Extension

Well, that's not really match "the second instance", but just "the last one",
but I hope for the sake of simpliniec this will be fine?


Explanation:

^(.+) >> match greedy from start one-or-more of any sign and store into group $1
File_Name - [Blah00!] - blah [ABC-123].ext

\s\[ >>> but only until we found the last (because greedy!) space-open Bracket combo,
followed by the rest ".+$" until the end, which we don't care about and want to drop, alas no need to capture into (...)
File_Name - [Blah00!] - blah [ABC-123].ext

Search for 'regex greedy lazy' to learn more about.


HTH? big_smile

- - -

To remove really "the second instance" and NOT "the last one",

from:
File_Name - [Blah00!] - blah [ABC-123] blup [XYZ99].ext
to:
File_Name - [Blah00!] - blah blup [XYZ99].ext
use:
Expre: ^(.+)\[.+?\] (.+)$
Repla: $1$2

Here
"(.+)\[" search greedy
".+?\]" search lazy due to the ?

.


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#3 2014-09-05 19:03

Shabono
Member
Registered: 2014-09-04
Posts: 3

Re: Looking to remove 2nd bit of text in square brackets in file names

Hi Stefan, and thank you for you quick reply. I see this is a little more complex than at first glance.

I did a few test runs with those and some worked OK but I also had some unintended changes. I need it be able to cope with any possible normal variations in the file name and just remove the second piece of text in square brackets (or last, as long as there are at least 2 in the file name). A few examples of the mistakes I got:

With the first expression:

(re-release) File Name - [random text - This & That 01] - Whatever [txt].txt
  became
(re-release) File Name -  - Whatever [txt].txt   1st instance was remove, not 2nd

Anthology - [Series Name 01] - This & That - Author.txt   was changed to:
Anthology -.txt

With the second expression:

Anthology - [Series Name 01] - This & That - Author.txt
was changed to:
Anthology -  - This & That - Author.txt

Offline

#4 2014-09-08 09:57

Shabono
Member
Registered: 2014-09-04
Posts: 3

Re: Looking to remove 2nd bit of text in square brackets in file names

Solved it, Thanks.

Search: (\[.*?\].*?)\[.*?\]
Replace: $1

Offline

#5 2014-09-08 16:22

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Looking to remove 2nd bit of text in square brackets in file names

>>>Solved it


Great! Well done big_smile




 


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#6 2020-05-24 17:21

Ptaker
Member
Registered: 2020-04-10
Posts: 13

Re: Looking to remove 2nd bit of text in square brackets in file names

This helped me also. Many thanks for the rule.

Offline

Board footer

Powered by FluxBB