You are not logged in.
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
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?
- - -
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
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
Solved it, Thanks.
Search: (\[.*?\].*?)\[.*?\]
Replace: $1
Offline
>>>Solved it
Great! Well done
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
This helped me also. Many thanks for the rule.
Offline