# Erb Regex?

**URL:** <https://www.ruby-forum.com/t/erb-regex/95575>\
**Category:** Rails\
**Created:** [June 6, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/erb-regex/95575 "2006-06-06T17:25:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chasconquest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chasconquest](https://www.ruby-forum.com/u/chasconquest)\
**Post date:** [June 6, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/erb-regex/95575/1 "2006-06-06T17:25:30Z")

</div>

Hi

Got a column named ‘sku’ in a table named ‘product’  
\<%= product.sku %\>

…looks like this… ‘1234\_56’

These last two chars (56) are an audio Track number.  
I ultimately want to display:  
Track \<%= product.sku %\>

I can isolate the first part with /\d+\_/ = 1234\_

But can’t figure out how to get the last two chars … 56

Can someone point me to a source for that shows how to parse strings  
like this in a Rails context?

THX

Chas

---

<div class="post-metadata">

**Author:** ![chasconquest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chasconquest](https://www.ruby-forum.com/u/chasconquest)\
**Post date:** [June 6, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/erb-regex/95575/2 "2006-06-06T19:01:11Z")

</div>

/\_(.+)/ should give you the latest two chars… If you need those  
numbers a lot, I would make a method in your model… something like:

def track()  
@sku =~ /\_(.+)/  
$1  
end

---

<div class="post-metadata">

**Author:** ![chasconquest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chasconquest](https://www.ruby-forum.com/u/chasconquest)\
**Post date:** [June 6, 2006, 10:24pm UTC](https://www.ruby-forum.com/t/erb-regex/95575/3 "2006-06-06T22:24:45Z")

</div>

Many thanks!!!

Wouter wrote:

> /\_(.+)/ should give you the latest two chars… If you need those  
> numbers a lot, I would make a method in your model… something like:
> 
> def track()  
> @sku =~ /\_(.+)/  
> $1  
> end

---

<div class="post-metadata">

**Author:** ![chasconquest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chasconquest](https://www.ruby-forum.com/u/chasconquest)\
**Post date:** [June 7, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/erb-regex/95575/4 "2006-06-07T12:55:37Z")

</div>

> > > > > “Chas” == Chas C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Can someone point me to a source for that shows how to parse strings  
> like this in a Rails context?

[http://www.oreilly.com/catalog/regex2/index.html](http://www.oreilly.com/catalog/regex2/index.html)

–  
Calle D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.livejournal.com/users/cdybedahl/](http://www.livejournal.com/users/cdybedahl/)  
“Ah, optimism. I remember optimism.” – Anya, Buffy the Vampire  
Slayer

---

<div class="post-metadata">

**Author:** ![chasconquest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chasconquest](https://www.ruby-forum.com/u/chasconquest)\
**Post date:** [June 7, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/erb-regex/95575/5 "2006-06-07T21:02:25Z")

</div>

Thanks Calle, I’m on it…

Wouter…Could you help me understand this one?  
How do i call this def track() method?

So here is what I think is going on:

the (object) “product”  
has an (attribute) “sku” which happens to have a value associated with  
“sku” of ‘xxxx\_xx’

so every time rails finds and displays all these attributes, I want to  
call:

def Track() in the store\_controller.rb - run the method and return the  
regexed value into the display.

But i just don’t “get” how to associate the block with def Track…

in the store\_controller.rb

def track()  
@sku =~ /\_(.+)/  
$1  
end

in my store/index.rhtml

\<% product.sku.track do |sku| %\>   
  
Track \<%= $1 %\>  
\<% end %\>

Thanks for your indulgence!  
Chas

---

<div class="post-metadata">

**Author:** ![chasconquest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chasconquest](https://www.ruby-forum.com/u/chasconquest)\
**Post date:** [June 8, 2006, 4:10am UTC](https://www.ruby-forum.com/t/erb-regex/95575/6 "2006-06-08T04:10:05Z")

</div>

well…I got it a different way.

seven characters in a string (ie. ‘0123\_56’)- I needed the last two as a  
audio “Track” reference.

this works in place without a method or regex component

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
