[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Extracting information from "john-2222-8888@domain.com"

Bob Sanders

10/25/2008 9:44:00 PM

Hello,

I'm wondering how you'd extract information from an email address like
"john-2222-8888@domain.com" to get these results from that email:

@player_id = 2222
@game_id = 8888

I'm thinking it's something along the lines of:

email = john-2222-8888@domain.com
@player_id = email.gsub(...)
@game_id = email.gsub(...)

Any ideas what belongs in the gsubs?
--
Posted via http://www.ruby-....

3 Answers

Matthew Moss

10/25/2008 9:51:00 PM

0

> I'm wondering how you'd extract information from an email address like
> "john-2222-8888@domain.com" to get these results from that email:
>
> @player_id = 2222
> @game_id = 8888
>
> I'm thinking it's something along the lines of:
>
> email = john-2222-8888@domain.com
> @player_id = email.gsub(...)
> @game_id = email.gsub(...)
>
> Any ideas what belongs in the gsubs?

Sounds more like a job for regexp pattern matching:

@player_id, @game_id = email.match(/.+-(\d+)-(\d
+)@domain.com/).captures
=> ["2222", "8888"]

@player_id
=> "2222"

@game_id
=> "8888"

You can get more specific with the regexp pattern; the one above I
just threw together quickly.


Bob Sanders

10/25/2008 10:10:00 PM

0

That works perfectly. Thank you, Matthew!!
--
Posted via http://www.ruby-....

William James

10/25/2008 11:16:00 PM

0

Bob Sanders wrote:

> "john-2222-8888@domain.com"

C:\>irb --prompt xmp
player,game = "john-2222-8888@domain.com".split(/[-@]/)[1,2]
==>["2222", "8888"]
player
==>"2222"
game
==>"8888"

--