Add a space before each capital letter in the file name

A swapping-ground for Regular Expression syntax

Add a space before each capital letter in the file name

Postby prlaba » Tue Jul 05, 2016 4:27 am

I need to 'correct' some of my MP3 music files that were created with all spaces in the filenames removed. For example, the MP3 file for the song 'I Feel The Earth Move' is named 'IFeelTheEarthMove.mp3' instead of ''I Feel The Earth Move.mp3'. Fortunately all words in the filenames are capitalized, so I can determine where the missing spaces need to be inserted (between two consecutive uppercase letters, or between a lowercase letter and the uppercase letter that immediately follows).

My first attempt was to use this regular expression:

Match: ([A-Za-z])([A-Z])
Replace: \1 \2

I expected this would locate any upper or lowercase letter followed immediately by an uppercase letter, and insert a space between the two letters, like this:

IFeelTheEarthMove.mp3 -> I Feel The Earth Move.mp3

(The above expressions worked as expected using another app called RegexRenamer. Unfortunately that app only lets you rename files one folder at a time, not a viable option for someone looking to rename files spread across hundreds of folders.)

But when I entered the above expressions, it replaced the entire filename with the just the first matching letter pair and inserted space:

IFeelTheEarthMove.mp3 -> I F.mp3

OK, I suppose that makes sense, since that is what the Replace expression says to do. I obviously need to also preserve the 'rest' of the filename before and after each matching letter pair.

So I next tried this:

Match: (.*[A-Za-z])([A-Z].*)
Replace: \1 \2

That did preserve the missing parts of the filename, but didn't exactly do what I wanted:

IFeelTheEarthMove.mp3 -> IFeelTheEarth Move.mp3

A single space was inserted between the last matching letter pair (the 'h' at the end of 'Earth' and the 'M' at the start of 'Move'). But what I wanted was to insert a space between every matching letter pair, not just one.

I suppose I could just apply this renaming multiple times to all of my files -- each 'apply' would insert one more space into each file's filename -- until all of the missing spaces were reinserted. But there must be a better way.

Can anyone suggest Match and Replace expressions that will insert a space between all matching letter pairs while preserving the rest of the filename?

Thanks.
prlaba
 
Posts: 1
Joined: Tue Jul 05, 2016 3:17 am

Re: Replace multiple occurrences?

Postby Admin » Thu Jul 07, 2016 3:42 pm

This can be done with a JavaScript function in BRU if you are interested. thanks
Admin
Site Admin
 
Posts: 1504
Joined: Tue Mar 08, 2005 8:39 pm

Re: Replace multiple occurrences?

Postby therube » Thu Jul 07, 2016 6:32 pm

As a start only, certainly not complete, & very likely would not be right in any case, but it does sort of separate the CAPS (to a point).

Something like this:

Code: Select all
Match:  ([A-Z){0,1}([^A-Z]*)([A-Z])([^A-Z]*)([A-Z])([^A-Z]*)([A-Z])([^A-Z]*)([A-Z])([^A-Z]*)
Replace:  \1 \2  \3  \4 \5 \6 \7 \8 \9


Seems to be a limit on the number of replacements \x ?
Not sure if it will handle "abcU"?
The Replace: so far is simply to give an idea of what is going on.
Some names will result in a replacement containing an opening space. Don't know offhand what actually may happen if that is actually written? Would the file name be " filename" or will the file name be named "filename" in any case?

I'm sure other issues will unfold.

Don't know enough, or haven't figured out enough to really go further then a method such as that?
(Often I don't really know just what might or might not be going on or why ;-).)
therube
 
Posts: 392
Joined: Mon Jan 18, 2016 6:23 pm

Re: Replace multiple occurrences?

Postby Admin » Thu Jul 07, 2016 10:33 pm

BRU Javascript :

Code: Select all
newName = name.replace(/([A-Z])/g, ' $1').trim()
Admin
Site Admin
 
Posts: 1504
Joined: Tue Mar 08, 2005 8:39 pm

Re: Add a space before each capital letter in the file name

Postby therube » Fri Jul 08, 2016 1:39 am

Oh, wow!
Then that is another method to solve, Add a space before each capital letter in the file name, as 7:Add -> Word Space does precisely that.


(LOL. One day I may just get around to reading the docs. Might help :-).)
therube
 
Posts: 392
Joined: Mon Jan 18, 2016 6:23 pm

Re: Add a space before each capital letter in the file name

Postby Admin » Fri Jul 08, 2016 4:22 pm

Yes, I also overlooked that one! :)
Admin
Site Admin
 
Posts: 1504
Joined: Tue Mar 08, 2005 8:39 pm


Return to Regular Expressions