Renaming files to only initals?

A swapping-ground for Regular Expression syntax

Renaming files to only initals?

Postby jumpman » Wed Mar 14, 2007 10:15 am

Hi. Wonder if anyone can help?
I'm looking for an expression to change files named:
jumpman one1
jumpman one2
jumpman two1

to:
j o1
j o2
j t1

Basically just the initals & the trailing number.
I bet regular expressions would do it, but I can't work it out! Thanks.
jumpman
 
Posts: 3
Joined: Tue Mar 13, 2007 5:40 pm

Postby Admin » Wed Mar 14, 2007 10:22 am

It if was "jumpman one1" to "jo" then it would be easy. Is it always the first initial of two words followed by the very last digit of the entire name?


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

Postby jumpman » Wed Mar 14, 2007 11:00 am

Thanks for responding.

It's always the first inital of the two words, but he digits are variable length, between 1 to 8 or so digits. I'm glad it's easy!
jumpman
 
Posts: 3
Joined: Tue Mar 13, 2007 5:40 pm

Postby Admin » Wed Mar 14, 2007 1:33 pm

Yeah, it's a straightforward expression:

Match: ([a-z])([a-z]*)(\s)([a-z])([a-z]*)(\d*)(\d)
Replace: \1 \4 \7

There are probably simpler solutions, but that one works okay.



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

Postby jumpman » Wed Mar 14, 2007 4:24 pm

Thanks. That worked brilliantly.
jumpman
 
Posts: 3
Joined: Tue Mar 13, 2007 5:40 pm

Postby Stefan » Thu Mar 15, 2007 8:49 am

Good work, Jim.

But you don't have to (group)
what you don't want to back refer to.

- - - - -

May I give an shorter example for the solution? (make an blank instead of ~)

Result gives the very last number:
(.).+~(.).+(\d)
\1~\2\3

- - -

Result gives the very first number:
(.).+~(.).+?(\d)
\1~\2\3

- - -

Result gives the whole number:
(.).+ (.).+?(\d+)
\1~\2\3


------------------

Explanation:

The ~ sign stands for: make an blank here instead of ~ sign!
The dot . means find one char.
.+ means find as many chars till there is an blank.
\d means search for numbers
\d+ means search for one or more numbers
question mark ? means find only as less as it's enough (non gready)

BTW: \s for blank signs is correct reg ex syntax, but not needed within BRU


HTH others
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Postby Admin » Thu Mar 15, 2007 9:40 am

Perfect, thank you.

Anyone who has read this forum for the last few years will know that I struggle with Regular Expressions :-)


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


Return to Regular Expressions