Skip to main content
added 17 characters in body
Source Link
Joseph R.
  • 40.6k
  • 8
  • 115
  • 146

You were close, but not quite:

rename -n 's/\[[^\]]+\]//' *.mp3 

The problem is that [] is a special regex construct and therefore the brackets need to be escaped if they are to match actual brackets. That's the purpose of the backslashes in my version.

The unescaped brackets form what is called a character class. A character class can basically match any of the characters inside the brackets. So [.*] matches a period or an asterisk. When you add a caret (^) to the beginning of a class, it matches any character not within the brackets.

In my regex, we are matching an opening bracket \[ plus one or more of non-closingcharacters that are not closing brackets [^\]]+ plus a closing bracket \] and removing all that.

You were close, but not quite:

rename -n 's/\[[^\]]+\]//' *.mp3 

The problem is that [] is a special regex construct and therefore the brackets need to be escaped if they are to match actual brackets. That's the purpose of the backslashes in my version.

The unescaped brackets form what is called a character class. A character class can basically match any of the characters inside the brackets. So [.*] matches a period or an asterisk. When you add a caret (^) to the beginning of a class, it matches any character not within the brackets.

In my regex, we are matching an opening bracket \[ plus one or more of non-closing brackets [^\]]+ plus a closing bracket \] and removing all that.

You were close, but not quite:

rename -n 's/\[[^\]]+\]//' *.mp3 

The problem is that [] is a special regex construct and therefore the brackets need to be escaped if they are to match actual brackets. That's the purpose of the backslashes in my version.

The unescaped brackets form what is called a character class. A character class can basically match any of the characters inside the brackets. So [.*] matches a period or an asterisk. When you add a caret (^) to the beginning of a class, it matches any character not within the brackets.

In my regex, we are matching an opening bracket \[ plus one or more characters that are not closing brackets [^\]]+ plus a closing bracket \] and removing all that.

Source Link
Joseph R.
  • 40.6k
  • 8
  • 115
  • 146

You were close, but not quite:

rename -n 's/\[[^\]]+\]//' *.mp3 

The problem is that [] is a special regex construct and therefore the brackets need to be escaped if they are to match actual brackets. That's the purpose of the backslashes in my version.

The unescaped brackets form what is called a character class. A character class can basically match any of the characters inside the brackets. So [.*] matches a period or an asterisk. When you add a caret (^) to the beginning of a class, it matches any character not within the brackets.

In my regex, we are matching an opening bracket \[ plus one or more of non-closing brackets [^\]]+ plus a closing bracket \] and removing all that.