RE: Re: erb Regex?


#1

Track <%= product.sku[-2,2] %> # -2 says “start at the
right, 2nd
place” , 2 says “get 2 characters”

all is fine! now I just have to get only 1 character if the
first is 0 -
‘0123_02’ - get 2 instead of 02

<%= /([1-9]?\d)$/.match(product.sku).to_s %>

not sure about it working in .rhtml but irb seems to work fine. There
probably a better way to represent it in Ruby but the regex works OK.


#2

Ross D. <Ross_Dawson@…> writes:

not sure about it working in .rhtml but irb seems to work fine. There
probably a better way to represent it in
Ruby but the regex works OK.

regex is ok, but i have a simple idea if you dislike regex.

product.sku[-2,2].to_i.to_s


#3

On Jun 7, 2006, at 11:00 PM, chenge wrote:

Ross D. <Ross_Dawson@…> writes:

all is fine! now I just have to get only 1 character if the
first is 0 -
‘0123_02’ - get 2 instead of 02

<%= /([1-9]?\d)$/.match(product.sku).to_s %>

regex is ok, but i have a simple idea if you dislike regex.

product.sku[-2,2].to_i.to_s

Damn! You beat me to it. ;->

That’s exactly what I was going to suggest, since it seems pretty
clear that what the OP is trying to get is “the number” that appears
at the end of that string, not just the “last two characters, that
happen to also be digits.”

-Brian