How to code numeric symbols in a string

Hello everyone!

(italics doesn’t mean anything in my message, it is just because of using {} in my text)

I’m very new to programming and macro language.
I have a list of TIFF images which I would like to rename.
names are:
P-01_1_DAPI
P-01_2_SIM
P-01_3_DAPI
P-01_4_SIM
etc.

I would like to put 0 before the counter to make:
P-01_01_DAPI, P-01_02_SIM etc.

I try this

dir1 = getDirectory("Choose Source Directory");
dir2 = getDirectory("Choose Destination Directory");
list = getFileList(dir1);
setBatchMode(true);
for (i=0; i<list.length; i++) {
showProgress(i+1, list.length);
open(dir1+list[i]);
title=getTitle();

if (matches(title, "P-0[0-9]_[0-9]_DAPI.*")) { 
name=replace(title, "_[0-9]_", "_0[0-9]_");
saveAs("TIFF", dir2+name);
close();}

if (matches(title, "P-0[0-9]_[0-9]_SIM.*")) { 
name=replace(title, "_[0-9]_", "_0[0-9]_");
saveAs("TIFF", dir2+name);
close();}
}

it doesn’t work and gives me files with “[0-9]” in names.
Is there a way to code numeric symbols in a string?
what should I use instead of [0-9]?
And what if I would like to code a short part of a string with numeric symbols of any length? Because I also have P-0101, P-010101 instead of P-01. And 23, 131 instead of 1,2,3,4…
For example I also use CellProfiler and there it is very easy:
P-[0-1]{*}_[0-1]{*}_(DAPI|SIM).
which = P- any number of numeric symbols _ any number of numeric symbols _ SIM or DAPI

Big thanks to everyone!:slight_smile:

Have a look at this post on string manipulation of images similar to what you want to do. indexOf() is your friend too, as is IJ.pad().

1 Like

Thank you!
I see new useful things for me.