# RE: Re: erb Regex?

**URL:** <https://www.ruby-forum.com/t/re-re-erb-regex/62789>\
**Category:** Rails\
**Created:** [June 8, 2006, 4:22am UTC](https://www.ruby-forum.com/t/re-re-erb-regex/62789 "2006-06-08T04:22:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ross\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Ross\_D](https://www.ruby-forum.com/u/Ross_D)\
**Post date:** [June 8, 2006, 4:22am UTC](https://www.ruby-forum.com/t/re-re-erb-regex/62789/1 "2006-06-08T04:22:21Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Ross\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Ross\_D](https://www.ruby-forum.com/u/Ross_D)\
**Post date:** [June 8, 2006, 5:02am UTC](https://www.ruby-forum.com/t/re-re-erb-regex/62789/2 "2006-06-08T05:02:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ross\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Ross\_D](https://www.ruby-forum.com/u/Ross_D)\
**Post date:** [June 8, 2006, 5:14am UTC](https://www.ruby-forum.com/t/re-re-erb-regex/62789/3 "2006-06-08T05:14:49Z")

</div>

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
