Moving last bit of filename to front?

A swapping-ground for Regular Expression syntax

Moving last bit of filename to front?

Postby Maf » Wed Mar 15, 2006 1:35 pm

Great utility, used it many times to great effect but have never used Regular Expressions and am having trouble getting to grips with it.

Can this be done...

Filenames end with a reference of letter, letter, number, number, number eg.

FilenameAB123.txt

I would like to rename the files so that the referance is at the beginning of the filename eg.

AB123Filename.txt

Any help would be greatfully appriciated

Maf
Maf
 
Posts: 2
Joined: Wed Mar 15, 2006 1:25 pm

Postby Admin » Wed Mar 15, 2006 4:16 pm

Hi Maf,

Sure, this is an easy one.

Match: (.*)(...\d\d)
Replace: \2\1

This says: I don't care how many letters are in the filename, I just care that it ends with three characters and two digits. Obviously this doesn't *explictly* handle situations where there's spaces or symbols in the filename, but I suspect it will match most situations for you.

HTH,


Jim

PS This example assumes you are using the latest version; if not then you'll have to handle the filename extension too.
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Postby Maf » Wed Mar 15, 2006 4:28 pm

Cheers Jim

Thanks for your speedy and very useful reply, has sorted me out superbly.

Now I just need to understand the expressions myself so I know how to do this type of thing in the future

Maf
Maf
 
Posts: 2
Joined: Wed Mar 15, 2006 1:25 pm

Postby remaner klub » Fri May 12, 2006 10:23 pm

I thought I would add to this topic, since it is close to what I want to do. RegExes drive me borderline insane and this is really getting to me.

I have hundreds of files that were named by someone who didn't think to name them so that they would appear in a chronologically-correct manner. The file name mask is:

filename-(m-d-yyyy)b.mp3

I simply want to change it to be:

filename-(yyyy-m-d)b.mp3

This seems simple, but I can't get it right. Please help if you can.

----

Also, it seems from the solution above that parenthesis are used to define groupings into \1, \2, etc. However, the help file refers to "brackets", which are [ and ], not parenthesis:

Notice we had two sets of brackets. Everything within each set of brackets is treated as a "grouping", and we refer to these groupings as \1, \2, \3 etc.
remaner klub
 
Posts: 7
Joined: Fri Apr 28, 2006 1:14 am

Postby remaner klub » Fri May 12, 2006 10:30 pm

I think I found the answer. Yes, 30 seconds after posing the question above.

Match:
Code: Select all
(.*)([\d]{1,2})-([\d]{1,2})-([\d]{4})(.*)


Replace:
Code: Select all
\1\4-\2-\3\5


I know regexes are powerful and handy and all that crap, but my god do they piss me off. Oh well thanks anyway.
remaner klub
 
Posts: 7
Joined: Fri Apr 28, 2006 1:14 am

Postby Admin » Fri May 12, 2006 10:42 pm

They are a total nightmare I agree. But sometimes they're a good thing....

I only use them when I really have to!



Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Postby remaner klub » Fri May 12, 2006 10:51 pm

I just found a problem. The regex I showed above is close, but not quite. And I was wrong about the order of the month and day numbers in the file names. The mask is actually:

filename-(d-m-yyyy)xxx.mp3

The match/replace I tried using was:

Match: (filename.*)([\d]{1,2})-([\d]{1,2})-([\d]{4})(.*)\.mp3

Replace: \1\4-\3-\2\5

But that ends up with the first digit of the day sometimes getting left behind. For example:

filename-(14-2-2006)b.mp3

Becomes:

filename-(12006-2-4)b.mp3

Please note the "1" belongs in front of the "4", not in front of "2006". Any ideas? I have to go pop some ortho-acetylsailycylic acid...

Edit: I found the problem. It's so lame I won't bother to include it here. It's not that I cry for help too soon; it's just that experience leaves me with little confidence that I can figure this stuff out, but once in awhile I do... Sorry.
remaner klub
 
Posts: 7
Joined: Fri Apr 28, 2006 1:14 am

Postby Admin » Sat May 13, 2006 7:51 am

No worries, that's what this forum is for - asking for help when you're stuck.


Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Re: Moving last bit of filename to front?

Postby baycom28 » Sun Mar 01, 2009 10:02 pm

Could you help me to move this text from bottom to front with regular expression?

Hotel California.Eagle.vob

to this

Eagle.Hotel California.vob

also there are also many song ending with artist in the bottom I want to move it to the front all the artist name seperate between the "."


Thanks,
baycom28
 
Posts: 1
Joined: Sun Mar 01, 2009 9:54 pm

Re: Moving last bit of filename to front?

Postby GMA » Mon Mar 02, 2009 6:28 am

Hi, baycom28:
This is what you need:

RegEx (1):
MATCH: (.*)(\.)(.*)
REPLACE: \3\2\1

Regards,

Gabriel.
GMA
 
Posts: 91
Joined: Sun Dec 02, 2007 1:30 pm
Location: Argentina


Return to Regular Expressions