Send emails from your address book

Our application has an invitation page in which the user can import his address book. We use an external service to receive them, so it simply displays the results in a text box. We started just breaking up the results with a comma and quickly found out that this would not work because of:

"Smith, Joe" < jsmith@example.com >, "Jackson, Joe" < jjackson@example.com > 

It would work between two records, but also split into them. Just wondering if there is a well-known way to prove your effectiveness.

Can regex work? I'm pretty bad for anyone to tell me which regular expression will only extract emails to an array ...

Something like that:

 emails = recipients.scan(/.*@.*/) <<==== but i know that not right 

EDIT

It seems like something like this might work. Does anyone have any suggestions if this will work for special occasions:

 emails = recipients.scan(/[a-z0-9_.-] +@ [a-z0-9-]+\.[az.]+/i) 
+4
source share
3 answers
 ruby-1.9.3-p0 :055 > a = '"Smith, Joe" < jsmith@example.com >, "Jackson, Joe" < jjackson@example.com >'; ruby-1.9.3-p0 :056 > b = a.scan(/<(.*?)>/).flatten => [" jsmith@example.com ", " jjackson@example.com "] ruby-1.9.3-p0 :057 > c = a.scan(/"(.*?)"/).flatten => ["Smith, Joe", "Jackson, Joe"] 

The index of the name / email address in each array is the same, so c [1] is the name for b [1] email.

Based on your comment, how about it:

 ruby-1.9.3-p0 :008 > a = '"Smith, Joe" < jsmith@example.com >, "Jackson, Joe" < jjackson@example.com >'; ruby-1.9.3-p0 :009 > b = '" test@domain.com , test2@domain.com "'; ruby-1.9.3-p0 :010 > b.scan(/\w*@\w*\.\w*/) => [" test@domain.com ", " test2@domain.com "] ruby-1.9.3-p0 :011 > a.scan(/\w*@\w*\.\w*/) => [" jsmith@example.com ", " jjackson@example.com "] 

This is almost the same as what you added to your question, just more compact.

+4
source

The Kassym version will fail under any circumstances, including any email addresses that contain non-word characters (for example, some.guy@gmail.com )

Parsing email lists cannot be done using regular expressions. Use something with a real parser, for example, mail gem:

 require "mail" Mail::AddressList.new(address_list).addresses.map(&:address) 

Ez!

+3
source

You can try to break into the following regular expression

 ,(?=(?:[^"]*"[^"]*")*[^"]*$) 

Altho is not an optimal fast solution and can be slow for long lines, it is better to use a specialized parser. Quoted quotes can be a problem with this solution, depending on how they are escaped (if at all).

0
source

Source: https://habr.com/ru/post/1384163/


All Articles