2011-03-05 5 views
0

CSVを電子メールでインポートするGEMは誰も知っていますか?Rails - 電子メールのCSVファイルをインポートする

課題となっているような電子メールのリストを与えられた:

"fname lname" <[email protected]>, "fname lname" <[email protected]>, "fname lname" <[email protected]>, "fname lname" <[email protected]>, "fname lname" <[email protected]>, "fname lname" <[email protected]>, "fname lname" <[email protected]>, "fname lname" <[email protected]>, "fname lname" <[email protected]> 

私は名前や電子メールだけでなく、電子メールの両方を抽出したいです。

アイデア?

+0

FasterCSVを試しましたか? – Shreyas

答えて

2

Rubyにはbuilt-in CSV supportがあり、CSVファイルの管理には、FasterCSVのような優れた宝石があります。つまり、表形式のデータを格納する「適切な」CSVではなく、コンマで区切られたデータのリストだけであるように見えます。あなたは、希望の日付を得るためにthis oneのような正規表現を使うことができます:

data = '"Some Guy" <[email protected]>, "Another Person" <[email protected]>, "One More" <[email protected]>' 
regex = /("([^"]*)" <([^>]*)>)/ 

data.scan(regex) do |match| 
    puts "Name: #{match[1]}, Email: #{match[2]}" 
end 

# Output: 

Name: Some Guy, Email: [email protected] 
Name: Another Person, Email: [email protected] 
Name: One More, Email: [email protected] 
+0

Brandonに感謝しますが、それは機能していないようです。ログに「{"authenticity_token" = "" XsU9VCt35gFB/ZQKDibUnXvX5d1LxytwenE + 1OiT74Y = "、" utf8 "=>"✓ "、" group_id "=>" 58 "、" emails "= > "[email protected]、[email protected]、[email protected]、Brad Smith <[email protected]>、[email protected]、joyce <[email protected]>"} – AnApprentice

+0

しかし、私がフォローすると、0試合があります。 @ emails_submitted = params [:emails] email_csv_regex = /( "([^"] *) "<([^> *)>)/ @ emails_submitted.scan(email_csv_regex)do |一致| Rails.logger.info 'Hello' Rails.logger.info "名前:#{一致[1]}、メール:#{一致[2]}" Rails.logger.info 'さようなら' end – AnApprentice

+0

また、このREGEXは、 tはFNAME LNAME <[email protected]>形式ではなく、[email protected]形式でしかありませんか? – AnApprentice

関連する問題