removing multiple instances of the same character

A swapping-ground for Regular Expression syntax

removing multiple instances of the same character

Postby bryn » Sat Mar 24, 2012 2:44 pm

First of all thank you for such a wonderful product! It has saved me tons of time working on organizing my mp3 collection.

I have been trying to learn how to do this regex stuff. I went though a lot of the tutorial topics on this forum and other peoples questions and answers and am unfortunately still confused how to do what I need. I have a bunch of files that are structured like this:

track_number - title.mp3

so for example:

01 - this is a song.mp3
02 - hey.mp3
03 - hey-hey.mp3

What I am trying to do is replace all instances of the "-" or hyphen character, except the first one, to a space. So back to the above example, only the 3rd song would be changed. It would change to:

03 - hey hey.mp3

The other 2 songs would not change as they only have 1 hyphen in them.

Is something like this possible with regex?

Thank you in advance for any suggestions or help.
bryn
 
Posts: 3
Joined: Sat Mar 24, 2012 2:38 pm

Re: removing multiple instances of the same character

Postby bitmonger » Mon Mar 26, 2012 9:15 am

A simple method would be to use Regex(1)
Match:
^([^-]+-)([^-]+)-(.+)$

^([^-]+-) - this captures everything from the start up to and including the first hyphen and saves it in \1

([^-]+) - this captures everything after the first hyphen up to the next hyphen and saves it in \2
- matches the second hyphen
(.+)$ - this captures everything from after the second hyphen (including any more hyphens) up to the end and saves it in \3

Replace:
\1\2 \3
note the space between \2 and \3. This replaces the hyphen which used to be there.

Include Ext. NOT checked

This will only match filenames with more than one hyphen. It replaces the second hyphen with a space. If you have filenames with more than 2 hyphens you simply run it more than once.
Let's say you have filenames with a range of 0 to 4 hyphens. It won't match those with 0 or one hyphen (and won't change them), but will match those with 2, 3, and 4. It will replace the second hyphen on the first pass. Now all the 2 hyphens become 1, the 3s become 2, and the 4s become 3.
A couple of passes and all are changed.

HTH
Bit
bitmonger
 
Posts: 50
Joined: Sat Sep 22, 2007 5:05 am

Re: removing multiple instances of the same character

Postby Stefan » Fri Mar 30, 2012 12:58 pm

bryn wrote:track_number - title.mp3

so for example:

01 - this is a song.mp3
02 - hey.mp3
03 - hey-hey.mp3

replace all instances of the "-" or hyphen character, except the first one, to a space.


I can imagine this two steps will work too.
Test it with test files first!


1.) replace ALL hyphen to space
Rename.
Result:
Code: Select all
01   this is a song.mp3
02   hey.mp3
03   hey hey.mp3



2.) replace THREE spaces "   " to " - "
Rename.
Result:
Code: Select all
01 - this is a song.mp3
02 - hey.mp3
03 - hey hey.mp3
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU


Return to Regular Expressions