简体   繁体   English

解析通讯簿中的电子邮件

[英]Parse emails from Address Book

Our application has an invite page where a user can import their address book. 我们的应用程序有一个邀请页面,用户可以在其中导入地址簿。 We're using an external service to get at them, so it just puts the results into a textarea. 我们正在使用外部服务来获取它们,因此它只是将结果放入文本区域。 We started out just splitting the results by comma, and quickly figured out that wasn't going to work because of: 我们开始只是用逗号分割结果,并很快发现由于以下原因无法正常工作:

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

It would work between the 2 entries, but also split inside them as well. 它可以在2个条目之间工作,但也可以在它们内部分开。 Just wondering if there's a well known fool-proof way to make this work. 只是想知道是否有一个众所周知的万无一失的方法来完成这项工作。

Maybe regex would work? 也许正则表达式会起作用? I'm pretty bad that, could anyone tip me off to what regex would extract just the emails into an array... 我很糟糕,有人可以告诉我正则表达式会将电子邮件提取到一个数组......

Something like this: 像这样的东西:

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

EDIT 编辑

Looks like something like this might work. 看起来像这样的东西可能会起作用。 Anyone have any suggestions if this would work for special cases: 如果这适用于特殊情况,任何人都有任何建议:

emails = recipients.scan(/[a-z0-9_.-]+@[a-z0-9-]+\.[a-z.]+/i)
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 name / email in each array is the same, thus c[1] is the name for the b[1] email. 每个数组中的名称/电子邮件索引是相同的,因此c [1]是b [1]电子邮件的名称。

Based on your comment how about his : 根据你的评论如何他的:

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"] 

Which is pretty much the same as you added to your question, just more compact. 这与您添加到问题中的几乎相同,只是更紧凑。

Kassym's version will fail in all sorts of circumstances, including on any email addresses that contain non-word characters (eg some.guy@gmail.com ) Kassym的版本将在各种情况下失败,包括任何包含非单词字符的电子邮件地址(例如some.guy@gmail.com

Parsing email lists can't be done with regular expressions. 使用正则表达式无法解析电子邮件列表。 Use something with a real parser, like the mail gem: 使用真正的解析器,如邮件 gem:

require "mail"

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

EZ! EZ!

You could try to split with the following regex 您可以尝试使用以下正则表达式进行拆分

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

Altho this is not an optimal quick solution, and could be slow for longs strings, better to use a specialized parser. 虽然这不是一个最佳的快速解决方案,并且对于longs字符串来说可能很慢,最好使用专门的解析器。 Quoted quotes could be a problem with this solution, depending on how they are escaped (if at all). 引用引号可能是此解决方案的问题,具体取决于它们如何转义(如果有的话)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM