Forum: Ruby on Rails RE: Re: erb Regex ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ross D. (Guest)
on 2006-06-08 06:22
(Received via mailing list)
> 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.
Chen G. (Guest)
on 2006-06-08 07:02
(Received via mailing list)
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
Brian H. (Guest)
on 2006-06-08 07:14
(Received via mailing list)
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
This topic is locked and can not be replied to.