views:

1137

answers:

1

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"/&gt;

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

+2  A: 

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" />
Ben Alpert
Does it work for months longer than 1 digit?
Manuel Ferreria
Yes, it'll just make sure the one-digit dates are padded first.
PEZ
Works like a charm!
Manuel Ferreria