tags:

views:

44

answers:

2

Hello. I'm trying to extract information from an email address like so in ruby:

Email: Joe Schmo <[email protected]>

to get these three variables:

name = ?
email = ?
domain = ?

I've tried searching everywhere but can't seem to find how to do it correctly. Here's what I have so far:

the_email_line = "Email: Joe Schmo <[email protected]>"

name = (/^Email\: (.+) <(.+)>@[A-Z0-9.-]+\.(.+)/).match(the_email_line)[1]
email = (/^Email\: (.+) <(.+)>@[A-Z0-9.-]+\.(.+)/).match(the_email_line)[2]
domain = (/^Email\: (.+) <(.+)>@[A-Z0-9.-]+\.(.+)/).match(the_email_line)[3]

Any idea on how to get those three variables correctly?

+4  A: 
/Email: (.+) <(.+?)@(.+)>/

Then grab what you want out of the capturing groups.

Anon.
You're awesome! Thank you Anon!!
sjsc
In particular, try `dummy, name, domain, email = (/Email: (.+) <(.+?)@(.+)>/).match(the_email_line).to_a`. You can throw away `dummy` (just a copy of the matched string).
bta
Better yet: `name, domain, email = (/Email: (.+) <(.+?)@(.+)>/).match(the_email_line).to_a[1..3]`
bta
A: 

I like this site when I want to try regex expressions in ruby: Rubular.com

Pran