How do I write this regex in ruby? (parsing Gmail API fields) -


i have 3 types of strings need parse:

"john smith <jsmith@gmail.com>"  "\"jsmith@gmail.com\" <jsmith@gmail.com>, \"bob@gmail.com\" <bob@gmail.com>"  "\"yo@gmail.com\" <yo@gmail.com>, john smith <jsmith@gmial.com>" 

i need hash of each, looks like:

{ 'john smith' => 'jsmith@gmail.com' } # first 1  { 'jsmith@gmail.com' => 'jsmith@gmail.com', 'bob@gmail.com' => 'bob@gmail.com' } # second 1  { 'yo@gmail.com' => 'yo@gmail.com', 'john smith' => 'jsmith@gmail.com' } # third 1 

you can use mail gem parse.

emails = "\"jsmith@gmail.com\" <jsmith@gmail.com>, \"bob@gmail.com\" <bob@gmail.com>, \"bobby\" <bobby@gmail.com>"  raw_addresses = mail::addresslist.new(emails)  result = raw_addresses.addresses.map {|a| {name: a.name, email: a.address}} 

the same thread: stackoverflow thread