Page 1 of 1

Rename special with RegExp

Posted: 10 Jun 2005 15:33
by RalphM
Since it took me a while to figure the correct syntax for my rename job, I thought I share it with you.

I had a couple of files named:

001.jpg
002.jpg
.
.
056.jpg


and I wanted just to insert some descriptive text at the beginning of the name, but keep the numbers as they were.

Here's the RegExp solution to do the job:
(to be entered in the "Rename special" "RegExp Rename")

(^\d{3}) > My_descriptive_text_$1

Explanation:

^ = Search at the beginning of the input string
\d = Search for digits
{3} = Search for exactly 3 digits
(...) = Capture the match for reuse
$1 = Reuse the captured match

Result:

My_descriptive_text_001.jpg
My_descriptive_text_002.jpg
.
.
My_descriptive_text_056.jpg


You see, it's worth to spend some time learning RegExp syntax, maybe for more sophisticated solutions.

Posted: 10 Jun 2005 17:34
by frew
Thanks for sharing that.

That's great.

How did you learn how to do regular expression work?

What's the main thing to keep in mind if one wants to learn how to do regular expression searches.

I don't know how t do that yet.

Thank you,

Frew

Re: Rename special with RegExp

Posted: 10 Jun 2005 19:02
by admin
Thanx, Ralph!
But wouldn't it have been easier to simply keep the last 7 chars of the names? Then you could even do:
Old_descriptive_text_001.jpg
Old_descriptive_text_002.jpg
...

New_descriptive_text_001.jpg
New_descriptive_text_002.jpg
...

by
(last 7 chars) > New_descriptive_text_$1

only: I don't know how to do "last 7 chars" in RegExp, do you?

EDIT: I think I got it: (.*)(.{7}$) > New_descriptive_text_$2
This will place "New_descriptive_text_" in front of the last 7 chars and drop everything that was before them. If the name is smaller than 7 chars it won't be changed at all. Test carefully, I'm not an expert in this!

Posted: 10 Jun 2005 20:25
by admin
frew wrote:How did you learn how to do regular expression work?
There's a lot in the web. This is nice: http://www.regular-expressions.info/

Posted: 10 Jun 2005 22:50
by frew
Thanks for the link.

Frew

Re: Rename special with RegExp

Posted: 14 Jun 2005 01:22
by RalphM
admin wrote: EDIT: I think I got it: (.*)(.{7}$) > New_descriptive_text_$2
This will place "New_descriptive_text_" in front of the last 7 chars and drop everything that was before them. If the name is smaller than 7 chars it won't be changed at all. Test carefully, I'm not an expert in this!
Will test it soon.
Probably just a different way to do almost the same.

Sharing this solution in the forum came to my mind primarly because it took me a while to figure, how to reuse the stored match.
(Apparently the \1...\n syntax - as described at the RegExp link from within the TV3 help - doesn't work here)

Thanks anyway, will keep you posted.