Topic: Extract between parentheses and pad different length numbers

FROM:
img_20080423095549.jpg
img_20080423095549(1).jpg
img_20080423095549(10).jpg
img_20080423095549(2).jpg

TO:
000.jpg
001.jpg
010.jpg
002.jpg

USE:
0000%b(s/.+\((\d+).+/\1/);%b(s/img_.+/0/)(-3)


EXPLANATION:
0000 -----------------------> Add a few padding zeros, no matter how much but as many as the shortest string needs.
%b(s/.+\((\d+).+/\1/)--> extract digits between (...)
; ---------------------------> separate different actions
%b(s/img_.+/0/) --------> rename file without (...) to 0
(-3) ------------------------> keep the last three signs from string only.


0000(-3) is used to pad numbers with different length (1 or 10 or 100) to the same end length (001 and 010 and 100)
First i add a many 0's so all strings have at least the length they should have, then i take the amount of signs i really want from the right.

.

Re: Extract between parentheses and pad different length numbers

This is a very interesting example and your solution is perfect.

For the users not fond of regular expressions here is another way to do it:
000%b[2,"()"].%e;%b(-3).%e

The explanation is very similar to the one you gave except that the extraction of the characters between the "()" is done with the "[]" modifier.

Thanks

3 (edited by Stefan 2011-12-09 23:57:21)

Re: Extract between parentheses and pad different length numbers

Works with Siren 3.0 too.


0000%b(s/.+\((\d+).+/\1/).%e;%b(s/img_.+/0/)(-3).%e

or
0000%b(s/.+\((\d+).+/\1/).%e;%f(s/img_.+/0/)(-7)

or
00000%b[2,")("].%e;%b(-3).%e

gives:
000.jpg
001.jpg
010.jpg
002.jpg



Explanation:

00000 ==> add padding zeros
%b ====> work on the base name
[2,")("] => return substring No. 2, use ")" and "(" as delimiter (the order of the delimiter doesn't matter)
.%e ===> add dot and original extension

Gives me:
00000.jpg
000001.jpg
0000010.jpg
000002.jpg


; =====> Siren delimiter for second expression

%b(-3).%e  ==> give me the last three signs from the right of the base name, then dot and extension

Gives:
gives:
000.jpg
001.jpg
010.jpg
002.jpg