use strict;
my $str = 'xxx@_domain.com
xxx@domain_.com
xxx@-domain.com
xxx@domain-.com
-xxx@domain.in
xxx@111.com
xxx@1and1.com
xxx@us.com
xxx@my-india.com
xxx@me2.com
xxx@tv.com
xxx@abc_efg.com
_muth@tamil.com
xx-u@my-india.commmmm
x-xx@1and1.com
xxx@_domain.com => invalid @ starts with _
xxx@domain_.com => invalid domain ends with _
xxx@-domain.com => invalid @ starts with - ( hypen)
xxx@domain-.com => invlaid domain ends with - (hypen)
-xxx@domain.in => invalid domain start with - (hypen)
xxx@111.com => invalid domain only contains numbers
xxx@1and1.com => valid domain contains both number and chars
xxx@us.com => valid
xxx@my-india.com => valid
xxx@me2.com => valid
xxx@tv.com =>
xxx@abc_efg.com => valid _ inside the chars
_muth@tamil.com => valid _ start with ';
my $regex = qr/^\w+(?:[_-][^\W_]+)*@(?![\d._-]+\.[^\W_]+$)[^\W_]+(?:[_-][^\W_]+)*\.[^\W_]{2,3}$/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