Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to add padding zeroes

I am working on this yahoo pipe Regex and I found a bug I'm unable to wrap my mind around it.

I have a URL, from which I extract digits, cat them and make a img html tag and embed it. The issue is that, the URL is presented in a non padded way, but the image linked has the zeroes. Therefore, when there is a day or a month with single digits, the regex stops working.

This is what I have so far:

The URL: http://www.penny-arcade.com/comic/2009/1/2/patently-ridiculous/
The RegEx: (\d{4})/(\d+)/(\d+)
The Replacement: <img src="http://www.penny-arcade.com/images/$1/$1$2$3.jpg" />

What should appear: <img src="http://www.penny-arcade.com/images/2009/20090102.jpg" />
What appears: <img src="http://www.penny-arcade.com/images/2009/200912.jpg"/>

How could I parse those zeroes as to make this thing work?

like image 219
Manuel Ferreria Avatar asked Nov 05 '22 22:11

Manuel Ferreria


1 Answers

If you can use more than one regular expression, here's a workaround:

search: (\d{4})/(\d)/
replace: $1/0$2/
search: (\d{4})/(\d{2})/(\d)/
replace: $1/$2/0$3/
search: (\d{2})/(\d{2})/(\d{2})/(.+)/
replace: <img src="http://www.penny-arcade.com/images/$1/$2$3.jpg" />
like image 194
Sophie Alpert Avatar answered Nov 15 '22 05:11

Sophie Alpert