sjsc
sjsc

Reputation: 4632

Regex: How do you get/extract information from an email using regular expressions (in ruby)?

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?

Upvotes: 3

Views: 1120

Answers (1)

Anon.
Anon.

Reputation: 60006

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

Then grab what you want out of the capturing groups.

Upvotes: 5

Related Questions