use strict;
my $str = 'John Smith <john.smith@gmail.com>
John Smith <johnsmith@gmail.com>
"John Smith" <johnsmith@gmail.com>
"John" <johnsmith@gmail.com>
John Smith<johnsmith@gmail.com>
<johnsmith@gmail.com>
johnsmith@gmail.com
mailto:johnsmith@gmail.com
"John"<johnsmith@gmail.com>
To: John Smith <john.smith@gmail.com>
From: John Smith <john.smith@gmail.com>
Reply-to: john.smith@gmail.com
Return-path: <john.smith@gmail.com>
Message-id: <john.smith@gmail.com>
References: <john.smith@gmail.com>
Original-recipient: rfc822;john.smith@gmail.com
for john.smith@gmail.com
ESMTPSA id <john.smith@gmail.com>
domain of john.smith@gmail.com
envelope-from=john.smith@gmail.com
(ORCPT john.smith@gmail.com)';
my $regex = qr/\s*(?:.*?[:=;]|ORCPT|for|domain of|ESMTPSA id)?\s*(?:"?([\w ]*?)[ "<])?\s*<?([\w.]*?@[\w.]*)>?/ip;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html