Forum: Ruby Extracting information from ""

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bob S. (Guest)
on 2008-10-26 01:44

I'm wondering how you'd extract information from an email address like
"removed_email_address@domain.invalid" to get these results from that email:

@player_id = 2222
@game_id = 8888

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

email = removed_email_address@domain.invalid
@player_id = email.gsub(...)
@game_id = email.gsub(...)

Any ideas what belongs in the gsubs?
Matthew M. (Guest)
on 2008-10-26 01:52
(Received via mailing list)
> @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
    => ["2222", "8888"]

    => "2222"

    => "8888"

You can get more specific with the regexp pattern; the one above I
just threw together quickly.
Bob S. (Guest)
on 2008-10-26 02:10
That works perfectly. Thank you, Matthew!!
William J. (Guest)
on 2008-10-26 02:20
(Received via mailing list)
Bob S. wrote:

> "removed_email_address@domain.invalid"

C:\>irb --prompt xmp
player,game = "removed_email_address@domain.invalid".split(/[-@]/)[1,2]
    ==>["2222", "8888"]
This topic is locked and can not be replied to.