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


#1

Hello,

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?


#2

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


#3

That works perfectly. Thank you, Matthew!!


#4

Bob S. wrote:

“removed_email_address@domain.invalid”

C:>irb --prompt xmp
player,game = “removed_email_address@domain.invalid”.split(/[-@]/)[1,2]
==>[“2222”, “8888”]
player
==>“2222”
game
==>“8888”