Removing First Set Of [xxx] In Filename

A swapping-ground for Regular Expression syntax

Removing First Set Of [xxx] In Filename

Postby metabru » Thu Jun 18, 2020 6:06 pm

Hello.
Long, long time user of BRU (for personal licence),
but first-time forum poster, because I'm stuck with this Regex situation.

I have dozens of files and I want to remove,
the first match of 'square brackets', e.g.
_____________________________
Before:
Cancer Research [Elsevier Publications Pty] Download-[ELSEVIER]-2020.pdf
After:
Cancer Research Download-[ELSEVIER]-2020.pdf
_____________________________
Before:
7 Habits Of Effective People [Self Help Books]-[KINDLE]-1992.epub
After:
7 Habits Of Effective People -[KINDLE]-1992.epub
_____________________________

So, I have dozens of these files, and I only want to remove
the first set of "square brackets" _and_ all words inside those square backets.
metabru
 
Posts: 3
Joined: Thu Jun 18, 2020 5:55 pm

Re: Removing First Set Of [xxx] In Filename

Postby metabru » Thu Jun 18, 2020 6:38 pm

From this
Remove all ( ) and characters within
-- viewtopic.php?f=3&t=2542&p=6697

I was able to get this working quite well:

Code: Select all
Remove(5) --> Crop --> Special:  [*]

However, I found a 'new' issue, when using the above ^

say, I have following files:
__________________
ABC-[I Am You]-[XYZ]-2002
LMNOP - [Duck Duck Go]-[DDG]-2009
BigBird [YELLOWSUB]-2019
__________________


I get this result:
__________________
ABC--[XYZ]-2002
LMNOP - -[DDG]-2009
BigBird -2019
__________________

I did not want the [YELLOWSUB] removed,
from BigBird [YELLOWSUB]-2019

Is there any way, to have a condition, e.g.
within the first set of square-brackets, find words/numbers which have a space ?
(because the second square-brackets will not have a space)

It's not a big deal, but just wondering.
metabru
 
Posts: 3
Joined: Thu Jun 18, 2020 5:55 pm

Remove 1st-set of [2-or-more words]

Postby RegexNinja » Fri Jun 19, 2020 1:01 am

I'm not clear on what the replacement should be for [xxx]'s separators?
Not sure if I'll be able to reply, so here's some regexes to try out:

^([^[]+?)[- _]+\[[^] ]+ .*?\][- _]*(.*)
\1 \2
Separators like [- _] ---> 1Space
aaa_ -[to remove]-- __[ToKeep]-AnyText ----> aaa [ToKeep]-AnyText

^([^[]+?)[- _]*([- _])\[[^] ]+ .*?\][- _]*(.*)
\1\2\3
With separators like [- _], only the one right-before [to remove] is preserved.
aaa_ -[to remove]-- __[ToKeep]-AnyText ----> aaa-[ToKeep]-AnyText
RegexNinja
 
Posts: 135
Joined: Fri Feb 21, 2020 5:26 pm

Re: Removing First Set Of [xxx] In Filename

Postby metabru » Fri Jun 19, 2020 3:31 pm

RegexNinja wrote:^([^[]+?)[- _]+\[[^] ]+ .*?\][- _]*(.*)
\1 \2

^([^[]+?)[- _]*([- _])\[[^] ]+ .*?\][- _]*(.*)
\1\2\3

RegexNinja,
you are the ninja.

Both of your Regex suggestions work well 8)
It's exactly what I needed.

In fact, I find it a bit strange, at how important the 'space' is in the Replace field,
e.g.
\1 \2 gives different results to \1\2
just like
\1\2\3 is different to \1 \2 \3

I thought with my basic Regex knowledge,
that would equate to the $1 $2 $3 variables, but I didn't realize that space would be crucial to getting the result I wanted !
metabru
 
Posts: 3
Joined: Thu Jun 18, 2020 5:55 pm


Return to Regular Expressions