Remove leading characters keeping name

A swapping-ground for Regular Expression syntax

Remove leading characters keeping name

Postby ocman » Wed Nov 02, 2011 10:13 pm

hey all, apologies if this has been posted, I've been searching and can't find it anywhere.

Here is the issue.
Thousands of music files that I have to rename, and its taking me a very long time.

Files that are like this:
12 - Duran Duran - Wild Boys.mp3

I want to be like this:
Duran Duran - Wild Boys.mp3

problem is its not always:
12 - Duran Duran

its also
04 - Duran Duran
02 - Fallout Boys
231 - Bruce Springsteen

any help would be appreciated.
Basically I want to remove anything, including spaces before the first Letter in the file name begins. No matter how many characters are before the name.

Thank you in advance for any help.
Posts: 1
Joined: Wed Nov 02, 2011 10:00 pm

Re: Remove leading characters keeping name

Postby Stefan » Fri Nov 04, 2011 11:16 pm


Match: '\d+ - (.+)'
Repla: \1

\d ===> match one digits
+ ===> match one-or-more of the last expression, here the \d
' - ' ===> match space, hyphen, space
(.+) ===> match one-or-more of any sign, and store it in group one, which match can be accessed by '\1' in the replacement.


I don't know why this works too in BRU. Because that is wrong expression, it should match only one digit, space, hyphen, space...:
Match: '\d - (.+)'
Repla: \1
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Remove leading characters keeping name

Postby Glenn » Sun Nov 06, 2011 6:17 pm

Hi Stefan,
Nice work on the regex.

I think the reason it (\d) still works is because the regex still matches part of the line.

for example, in the line below the \d - will match the 2 - . The (.+) matches the rest.

12 - Duran Duran - Wild Boys.mp3

It's true that it doesn't match the whole line but we aren't capturing anything before the \d so it is ignored and dropped when the replacement takes place.
i.e. as long as the complete regex matches within the line it will work, even if that match doesn't include the whole line.

It does make a difference (won't match) if we change the regex to
^\d - (.+)
In this case it is trying to force a match with a single leading digit beginning at the start of the line.
This is probably what you were thinking when you wondering why it would still work with a single \d.

Posts: 28
Joined: Fri Apr 14, 2006 4:53 pm
Location: Winnipeg, Canada

Re: Remove leading characters keeping name

Postby Offspring » Mon Mar 23, 2015 4:11 pm

Sorry to resurrect an old thread, but I'm running into a similar problem. I've got a ton of files named anywhere from 1) through 3639) so like 1) Origin 01.cbz. I tried using the regex above but it didn't seem to take. How can I remove all of the numbers, the parenthesis and the space before the file name while keeping the file name in tact?

Posts: 1
Joined: Mon Mar 23, 2015 4:08 pm

Re: Remove leading characters keeping name

Postby bitmonger » Tue Mar 31, 2015 6:21 pm

It won't work because the original match was for a number followed by a space , a dash, and a space, then the rest of the title.
Is that what you are trying to match? No, you are trying to match a number followed by a parenthesis, so the Regex string has to match that.
To stay with Stefan's expression, try
\d\) (.+)

Be aware that this matches exactly a number, a parenthesis, and then a single space.
It will not match if there is not a space following the parenthesis.
The reason for the \) in the match instead of just a ) is that the parenthesis has a special meaning in regular expressions, and the backslash means use it literally.

Hope this helps
Posts: 50
Joined: Sat Sep 22, 2007 5:05 am

Return to Regular Expressions