Rename special with RegExp

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
RalphM
Posts: 1935
Joined: 27 Jan 2005 23:38
Location: Cairns, Australia

Rename special with RegExp

Post 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.
Ralph :)
(OS: W11 22H2 Home x64 - XY: Current beta - Office 2019 32-bit - Display: 1920x1080 @ 125%)

frew
Posts: 75
Joined: 02 Feb 2005 17:48

Post 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

admin
Site Admin
Posts: 60588
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Rename special with RegExp

Post 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!

admin
Site Admin
Posts: 60588
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Post 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/

frew
Posts: 75
Joined: 02 Feb 2005 17:48

Post by frew »

Thanks for the link.

Frew

RalphM
Posts: 1935
Joined: 27 Jan 2005 23:38
Location: Cairns, Australia

Re: Rename special with RegExp

Post 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.
Ralph :)
(OS: W11 22H2 Home x64 - XY: Current beta - Office 2019 32-bit - Display: 1920x1080 @ 125%)

Post Reply