Topic: Pad Regular Expression tags

I've just started to try Siren but am struggling to work out how to do what I want (I wish there were more examples).


I want to convert filenames like: 30-3-2013 - Fred was here.txt (begins day, month, year) to 2013-03-30 - Fred was here.txt


My Expression so far is: %b(s/([0-9]+)-([0-9]+)-([0-9]+)/\3-\2-\1 \4/).%e

That gets it to: "2013-3-30  - Fred was here.txt", close but I want the month (3) padded to "03".

I'm no regular expression expert either :-)
If someone can explain why this doesn't work that would be great:   %b(s/(.+)-(.+)-(.+)/\3-\2-\1 \4/).%e (possibly greedy verus non-greedy, if so any way to control that?

Re: Pad Regular Expression tags

Hello and welcome,

It seems that the problem here is padding with a regular expression.
Using two of them should do the trick. For example :
%b(s/-(\d)-/-0\1-/g)(s/([0-9]+)-([0-9]+)-([0-9]+)/\3-\2-\1 \4/).%e
the first regular expression "s/-(\d)-/-0\1-/g" will pad one digit substrings with a "0" and give its result to the second one (yours).

But you can achieve this without regular expressions. For example with this one :
%N3-%N2{2}-%N1{2} - %b[-1].%e

%N3 : extracts the 3rd number of the base name
%N2{2} : extracts the 2nd one and pad it to two digits
%N1{2} : extracts the 1st one and pad it to two digits
%b[-1] : extract the last element from the base name when considering that elements are separated by "-"

Hope this will help,

Rémi

Re: Pad Regular Expression tags

Hi and welcome!

dbareis wrote:

My Expression so far is: %b(s/([0-9]+)-([0-9]+)-([0-9]+)/\3-\2-\1 \4/).%e

That gets it to: "2013-3-30  - Fred was here.txt", close but I want the month (3) padded to "03".

>>(I wish there were more examples)
>>If someone can explain why this doesn't work that would be great: 

Some tips:

1) RegEx is a pattern matching system to take some parts to keep and maybe some others to drop.
So one would e.g. match parts of the file name, store them in backreference groups and reorder them in the output.
As you did. But there is no magic as conjure up additional parts out of nowhere.

There is no such things as "padding" per se. Just matching parts and use them for some operations. As Rémi did with his
first additional expression s/-(\d)-/-0\1-/g (match single digit between hyphens and replace with '0' and matched digit)
Then you can continue whit that first output as input for the reorder-expression of you.

2) Parentheses () can be used to store what is matched for later reuse by \1 and so on.
Those backreference groups are counted from left to right.
So s/([0-9]+)-([0-9]+)-([0-9]+ will provide three backreference groups,... but you use *four* references \3-\2-\1 \4
The \4 is to much, because not matched, and not needed either as Siren provide the unmatched part on its own.

3) Instead of [0-9] you can use the shorter \d
The character class [0-9] would be used as e.g. [1-3] to match 1,2 or 3 only.
But to match any digit out of 0,1,2,3...9 you can use just \d


So your expression could be written as s/(\d+)-(\d+)-(\d+)/\3-\2-\1


That you do not need to match the text too is because which regex engine is used,
and how Rémi has implemented that.


HTH? big_smile

Re: Pad Regular Expression tags

Thanks for those replies. I tried the suggestions and they work well.   They will help me get started, it will take a bit to sink in but I think it will be worth learning  smile