0

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.

SoAwesomeMan
  • 3,226
  • 1
  • 22
  • 25
desioner
  • 11
  • 3

3 Answers3

0

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)
  ]            ']'
hwnd
  • 69,796
  • 4
  • 95
  • 132
  • This did exactly what I wanted to happen. While the syntax is different from the previous one I'll keep this one handy. Thank you for the quick response and accurate regex! – desioner Apr 18 '14 at 02:14
0

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.

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • 1
    Actually `[^\]]` is a syntax error in some dialects, and means any character except backslash or close square bracket in others, or any character except backslash followed by a literal right square bracket in yet other dialects. – tripleee Apr 16 '14 at 07:03
  • @tripleee I didn't know it. in which dialect? I used `\ ` to distinguish it, otherwise there are lot of `]` around feels like `/(o_O)\` – Sabuj Hassan Apr 16 '14 at 07:06
  • Commonly `[^]]` means not a literal right square bracket. The first character of the character class (after a possible negation) is always literal. This is the case in GNU and POSIX grep, Perl, most Sed dialects, Awk, PCRE, etc. – tripleee Apr 16 '14 at 07:12
  • This did exactly what I wanted to happen. While the syntax is different from the following one I'll keep this one handy. Thank you for the quick response and accurate regex! – desioner Apr 18 '14 at 02:15
0

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.     
Community
  • 1
  • 1
SoAwesomeMan
  • 3,226
  • 1
  • 22
  • 25
  • This didn't do what I wanted to have happen. I'm not sure what it would do but thank you for the reply. – desioner Apr 18 '14 at 02:11
  • @desioner Glad you found a working answer. I added tags and a link to the question to clarify it uses [tag:regex] with [tag:find-and-replace]. Although not working for your application, my answer's regex provides three match groups: 1) any char until second match group—group is returned; 2) brackets and chars within–NON-[tag:capturing-group], thus group not returned; and 3) any char after second match group—`?` quantifier makes group not required, thus it's returned if it exists. Joining returned groups would get you the desired filenames. More info: http://regex101.com/r/oY4aT8 – SoAwesomeMan Apr 18 '14 at 03:44
  • Ok, thank you for the additional information. Regex is a new world for me and although I'll never really get to use it I see it's value. I'm sure that I'll be needing the help of capture groups soon enough. – desioner Apr 18 '14 at 12:49