note
haukex
<p>The usual "don't use a regex when a real parser exists" applies here too. Single quotes are <i>technically</i> a valid character in email addresses, as are commas and spaces when quoted. The following uses [mod://Email::Address] to correctly parse and split such (admittedly very unusual and [https://datatracker.ietf.org/doc/html/rfc5321#page-42|not recommended]) addresses.</p>
<c>
use warnings;
use strict;
use Email::Address;
while (<DATA>) {
for my $addr (Email::Address->parse($_)) {
print $addr->address, "\n";
}
}
__DATA__
'me'@here.com, "West, Casey" <casey@localhost>
"those,foo"@there.com others@there.com
you@there.com,them@there.com
"Hello, World"@example.com
</c>
<p>Outputs:</p>
<c>
'me'@here.com
casey@localhost
"those,foo"@there.com
others@there.com
you@there.com
them@there.com
"Hello, World"@example.com
</c>
11137286
11137286