How to remove text between and including brackets?

A swapping-ground for Regular Expression syntax

How to remove text between and including brackets?

Postby trw » Sat Apr 29, 2006 6:24 am

Can someone show me how to remove bracketed text in filenames?

My Name Is Sam [Not Really] Spade.txt

to

My Name Is Sam Spade.txt

Thank you!
trw
 
Posts: 2
Joined: Sat Apr 29, 2006 6:20 am

Postby Glenn » Mon May 08, 2006 4:31 pm

This should be reasonably straightforward.
First let's break it down

We don't care what precedes the [
(.*)

Then we want to separate out the bracketed section
Since brackets are meta characters we need to escape them so start
(\[
We don't care what is between them until we hit the closing bracket so add .*
Now we have
(\[.*

Now the closing bracket
\])
Gives us
(\[.*\])

We don't care what is after the brackets so capture it in (.*)
Put it all together and we get:
(.*)(\[.*\])(.*)

As replacement we want the first and third group (the second group has the bracketed text) so replace with \1\3

Using it on:
My Name Is Sam [Not Really] Spade
changes it to:
My Name Is Sam Spade

There is a double space between Sam and Spade - it may not show in this post but will be obvious in your result

We'll get rid of that by capturing the leading space before the bracket
which give this:
(.*)( \[.*\])(.*)

So in Regex(1)
In Match put: (.*)( \[.*\])(.*)
In Replace put: \1\3

This will work fine, but it does a lot of backtracking to get the job done.
It takes about 126 steps to match the sample line. (This would only be an issue if doing thousands of file renames.)
If you use a match of: ([^\[]+)( \[[^\]]*\])(.*) it would get a match in 14 steps - it uses ^\[ and ^\] which will match anything but the bracket as a wildcard instead of .*
Either will work with the \1\3 as replace text.

Hope this helps
Glenn
Last edited by Glenn on Tue May 09, 2006 6:48 am, edited 1 time in total.
Glenn
 
Posts: 28
Joined: Fri Apr 14, 2006 4:53 pm
Location: Winnipeg, Canada

Thank you Glenn!

Postby trw » Mon May 08, 2006 5:01 pm

:D
trw
 
Posts: 2
Joined: Sat Apr 29, 2006 6:20 am

Re: How to remove text between and including brackets?

Postby jesoakley » Wed Feb 04, 2009 12:48 pm

Pretty good, but could be even better if it took into account leading brackets e.g. [My Name Is] Sam Not Really Spade.txt.
This doesn't match because there is no leading space, but if you remove the space from the expression, you leave it in the result. Is there any way around this? maybe it is possible to trim leading/trailing spaces and compress embedded double-spaces to a single space? Perhaps using section 3 or 5. I have only just downloaded this app, and it is excellent, but not that intuitive. Can anyone help?
jesoakley
 
Posts: 2
Joined: Wed Feb 04, 2009 12:40 pm

Re: How to remove text between and including brackets?

Postby jesoakley » Wed Feb 04, 2009 12:56 pm

Yeah, if you remove the space from (.*)( \[.*\])(.*) , taking it back to (.*)(\[.*\])(.*) it then picks up leading brackets too.
Then in section 3 replace two spaces with one space.
Then in section 5 click the trim checkbox.
That seems to do it.
jesoakley
 
Posts: 2
Joined: Wed Feb 04, 2009 12:40 pm

Re: How to remove text between and including brackets?

Postby dstarfire » Fri Sep 11, 2009 1:00 am

How would you remove any brackets, and the text they contain, regardless of where in the name they appear? I download a lot of fansubs which are usually named in the format [encoder's name]series name - episode name [other stuff that I don't][care about]. The tags can appear before or after the actual name, and there can be any number of them.

Ideally, I just want to delete anything matching the regex (\[[^\]]+\]), but I can't seem to find a way to do this.

*edit*
Okay, I answered my own question, and I feel really stupid. This is explicitly mentioned as an example in the help file. Anyways, to remove any set of brackets and the text they contain, use the CROP option with the string [*] (including the brackets).
dstarfire
 
Posts: 1
Joined: Sun Sep 06, 2009 6:05 pm


Return to Regular Expressions