use strict;
my $str = '--------- valid ---------
something@something.com
someone@localhost.localdomain
someone@127.0.0.1
a@b.b
a/b@domain.com
{}@domain.com
m*\'!%@something.sa
tu!!7n7.ad##0!!!@company.ca
%@com.com
!#$%&\'*+/=?^_`{|}~.-@com.com
USER@EXAMPLE.COM
someone@do-ma-in.com
a@p.com
-------- invalid --------
somebody@example
a@p.com
.wooly@example.com
wo..oly@example.com
invalid:email@example.com
@somewhere.com
example.com
@@example.com
a space@example.com
something@ex..ample.com
a\\b@c
----- empty string ----
-------- space --------
someone@somewhere.com.
\\"\\"test\\blah\\"\\"@example.com
\\"testblah\\"@example.com
someone@somewhere.com@
someone@somewhere_com
someone@some:where.com
.
F/s/f/a@feo+re.com
some+long+email+address@some+host-weird-/looking.com
a @p.com
a\\u0020@p.com
a\\u0009@p.com
a\\u000B@p.com
a\\u000C@p.com
a\\u2003@p.com
a\\u3000@p.com
ddjk-s-jk@asl-.com
someone@do-.com
somebody@-p.com
somebody@-.com';
my $regex = qr/^([a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?)$/mp;
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