Moving Numbers to the start of a file name

A swapping-ground for Regular Expression syntax

Moving Numbers to the start of a file name

Postby DDF2K2 » Fri Feb 03, 2012 3:45 pm

Hi

I am looking for some help moving numbers in a file name to the begining. I have folders set up my PC which contain various files. I want to move the numbers to the begining of the file but leave the rest intact

My Filenames look like
DNS-24597-HTD.pdf
PO-24567 VXI.pdf
POS-24655-XfD.pdf

I would like them to be

24597-DNS--HTD.pdf
24567-PO- VXI.pdf
24655-POS-XfD.pdf

I do have a slight problem in that not all files have the dashes. For example some files might look like

PO 24604 EMM.pdf

I am a little lost with respect to writing the expressions.

Thanks
Nick
DDF2K2
 
Posts: 3
Joined: Fri Feb 03, 2012 3:40 pm

Re: Moving Numbers to the start of a file name

Postby Stefan » Fri Feb 03, 2012 10:39 pm

regex have f.ex. this options:

? >>>>>>> to match one-or-none
(x|y) >>> alternative OR matching
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Moving Numbers to the start of a file name

Postby DDF2K2 » Mon Feb 06, 2012 10:33 am

Thanks for the response Stefan,

I cannot get it to work however. I think I must be doing something stupid!

Currently I have
Match
(.*) (\d+) (.*)
Replace
\2 \1 \3
But is does nothing!
DDF2K2
 
Posts: 3
Joined: Fri Feb 03, 2012 3:40 pm

Re: Moving Numbers to the start of a file name

Postby bitmonger » Sat Feb 25, 2012 9:12 am

You are very close to solving the problem. In fact, the sample you gave worked for me but only on the file called PO 24604 EMM.pdf.
The problem is that you need to have something that will match either the - or the space on both ends of the number. The one you tried will match the space but not the -. Stephan suggested using (x|y) which is a good idea, and in this case would have been ( |-).
So then your regular expression would be:
(.*)( |-)(\d+)( |-)(.*)
and you would have 5 captured groups, so the replacement would be \3\1\5.
If you wanted to make the alternating groups non-capturing you could use:
(.*)(?: |-)(\d+)(?: |-)(.*) and then use the \2\1\3 replacement.
This looks more complicated, and here's an alternative and simpler looking option. Consider that what you want to do is split the name up based on the fact the number in the center is bounded by 2 non-numbers. As you correctly used, a number is represented by the \d. A non-number is represented by a \D i.e. a capitol D. Both a space and a dash are non-numbers and would therefore match.
Try (.*)\D(\d+)\D(.*) as your match and \2 \1 \3 as you replace ( or \2-\1-\3 if you want the result to have dashes separating the groups).
Make sure the check box at the top of the RegEx(1) is checked and the Include Ext. is NOT checked.
Also, make sure you actually select the files you want renamed. Users sometimes don't see a change because they haven't selected the files with the mouse.
This solution worked fine on my computer with your filenames.

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

Re: Moving Numbers to the start of a file name

Postby DDF2K2 » Mon Feb 27, 2012 11:40 am

Perfect (.*)\D(\d+)\D(.*) Did the trick! Thanks for the help!
DDF2K2
 
Posts: 3
Joined: Fri Feb 03, 2012 3:40 pm


Return to Regular Expressions