根据我的理解,在给定文档中扫描电子邮件时,正则表达不是最好的选择.我想知道这有什么替代方案吗?或者一些我不知道的最佳实践方式?
解决方法
对于解析作业,依赖库是一个好主意.你是对的,考虑到不同的情况等,图书馆总是比正则表达式更详细地处理问题.
Mail is an internet library for Ruby that is designed to handle emails
generation,parsing and sending in a simple,rubyesque manner.[…] Mail has been designed with a very simple object oriented
system that really opens up the email messages you are parsing,if you
know what you are doing,you can fiddle with every last bit of your
email directly.
mail = Mail.read('/path/to/message.eml') mail.envelope.from #=> 'mikel@test.lindsaar.net' mail.from.addresses #=> ['mikel@test.lindsaar.net','ada@test.lindsaar.net'] mail.sender.address #=> 'mikel@test.lindsaar.net' mail.to #=> 'bob@test.lindsaar.net' mail.cc #=> 'sam@test.lindsaar.net' mail.subject #=> "This is the subject" mail.date.to_s #=> '21 Nov 1997 09:55:06 -0600' mail.message_id #=> '<4D6AA7EB.6490534@xxx.xxx>' mail.body.decoded #=> 'This is the body of the email...