我需要解析 3 种类型的字符串:
"John Smith <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="39534a54504d51795e54585055175a5654" rel="noreferrer noopener nofollow">[email protected]</a>>"
"\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="066c756b6f726e46616b676f6a2865696b" rel="noreferrer noopener nofollow">[email protected]</a>\" <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5cfd6c8ccd1cde5c2c8c4ccc98bc6cac8" rel="noreferrer noopener nofollow">[email protected]</a>>, \"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9bf9f4f9dbfcf6faf2f7b5f8f4f6" rel="noreferrer noopener nofollow">[email protected]</a>\" <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92f0fdf0d2f5fff3fbfebcf1fdff" rel="noreferrer noopener nofollow">[email protected]</a>>"
"\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="641d0b240309050d084a070b09" rel="noreferrer noopener nofollow">[email protected]</a>\" <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81f8eec1e6ece0e8edafe2eeec" rel="noreferrer noopener nofollow">[email protected]</a>>, John Smith <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8c2dbc5c1dcc0e8cfc5c1c9c486cbc7c5" rel="noreferrer noopener nofollow">[email protected]</a>>"
我需要每个的哈希值,如下所示:
{ 'John Smith' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c263f212538240c2b212d2520622f2321" rel="noreferrer noopener nofollow">[email protected]</a>' } # for the first one
{ '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c061f010518042c0b010d0500420f0301" rel="noreferrer noopener nofollow">[email protected]</a>' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dab0a9b7b3aeb29abdb7bbb3b6f4b9b5b7" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c5e535e7c5b515d5550125f5351" rel="noreferrer noopener nofollow">[email protected]</a>' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="44262b26042329252d286a272b29" rel="noreferrer noopener nofollow">[email protected]</a>' } # for the second one
{ '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e1988ea1868c80888dcf828e8c" rel="noreferrer noopener nofollow">[email protected]</a>' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7aeb897b0bab6bebbf9b4b8ba" rel="noreferrer noopener nofollow">[email protected]</a>', 'John Smith' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="513b223c38253911363c30383d7f323e3c" rel="noreferrer noopener nofollow">[email protected]</a>' } # for the third one
最佳答案
您可以使用mail要解析的 gem。
emails = "\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9d3cad4d0cdd1f9ded4d8d0d597dad6d4" rel="noreferrer noopener nofollow">[email protected]</a>\" <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c465f414558446c4b414d4540024f4341" rel="noreferrer noopener nofollow">[email protected]</a>>, \"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f3d303d1f38323e3633713c3032" rel="noreferrer noopener nofollow">[email protected]</a>\" <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3b1bcb193b4beb2babffdb0bcbe" rel="noreferrer noopener nofollow">[email protected]</a>>, \"Bobby\" <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d2f222f2f340d2a202c2421632e2220" rel="noreferrer noopener nofollow">[email protected]</a>>"
raw_addresses = Mail::AddressList.new(emails)
result = raw_addresses.addresses.map {|a| {name: a.name, email: a.address}}
同一线程:stackoverflow thread
关于ruby - 我如何在 ruby 中编写这个正则表达式? (解析 Gmail API 字段),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29907033/