Question

I'm using the Mac App Name Mangler. It allows the use of regex. I have a bunch of files and folders that contain some thing like the following.

File Name [24.9MB].ext
Folder Name [1GB]

I want the regex to remove the brackets and what's between them. Also if there's a space before the brackets would be nice if it was deleted as well. Thus becoming...

File Name.ext
Folder Name

I've read a bit about regex now and realized that the bracket itself is part of the syntax. I need to get around that and what is contained in them. Any help would be appreciated. Having to manually cleanup hundreds of thousands of files and folder names would be too much.

Was it helpful?

Solution

Using the Find and Replace function:

Find: \s*\[[^]]*]
Replace with: 

Regular expression:

\s*            whitespace (\n, \r, \t, \f, and " ") (0 or more times)
 \[            '['
  [^]]*        any character except: ']' (0 or more times)
  ]            ']'

OTHER TIPS

To match the brackets and it's content, the regex will be \[[^\]]+\]. Now if you want to catch the space before the bracket, then you can use [ ]*. It means zero or more spaces.

So, combining the both, your search regex will be:

[ ]*\[[^\]]+\]

FYI: [^\]]+ means any character except the ] having one or more times.

Use a "non capture group" for the brackets: What is a non-capturing group? What does a question mark followed by a colon (?:) mean?

http://rubular.com/r/bVhVbKomKm

/(.+)(?:\s?\[[^\]]+\])(.+)?/

# Match 1
# 1.    File Name
# 2.    .ext
#
# Match 2
# 1.    Folder Name
# 2.     
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top