Trying to catch invalid emails

S

Samik R.

Hello,
I use the following regular expression to catch typical invalid email
addresses:
------------
my @Email=("sam._\@abc.org", "sam_.\@abc.org", "sam_.\@abc.org");
foreach (@Email)
{

if(/^[A-z0-9]+([_\.][A-z0-9\-]+)*[@][A-z0-9_\-]+([.][A-z0-9_\-]+)?\.[A-z]{2,3}$/)
{ print "$_ is a valid email id\n"; }
else
{ print "$_ is an invalid email id\n"; }
}
-------------

This expression does not catch the above 3 emails in the array (the
program says that they are valid emails).

Can someone help me to discard these three?
Thanks.
 
R

Ron Bergin

Hello,
I use the following regular expression to catch typical invalid email
addresses:
------------
my @Email=("sam._\@abc.org", "sam_.\@abc.org", "sam_.\@abc.org");
foreach (@Email)
{

if(/^[A-z0-9]+([_\.][A-z0-9\-]+)*[@][A-z0-9_\-]+([.][A-z0-9_\-]+)?\.[A-z]{2,3}$/)
{ print "$_ is a valid email id\n"; }
else
{ print "$_ is an invalid email id\n"; }}

-------------

This expression does not catch the above 3 emails in the array (the
program says that they are valid emails).

Can someone help me to discard these three?
Thanks.

Try using the Email::Valid module.

use strict;
use warnings;
use Email::Valid;

my @Email=("sam._\@abc.org", "sam_.\@abc.org", "sam_.\@abc.org");

foreach my $address (@Email)
{
print Email::Valid->address($address) ? "$address valid\n" :
"$address not valid\n";
}
 
B

Ben Morrow

Quoth "Samik R. said:
Hello,
I use the following regular expression to catch typical invalid email
addresses:

Don't do that. Use a module, such as Email::Valid, that actually gets it
right.

Ben
 
B

Ben Bullock

Samik R. said:
if(/^[A-z0-9]+([_\.][A-z0-9\-]+)* ...

The problem is A-z here, A-z contains all of

ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz

so it matches the underscore in the email address.

To match only letters, you should use [A-Za-z0-9] instead.
 
G

Gordon Etly

Abigail said:
_
Frank Seitz ([email protected]) wrote on VCCCLXIII September
MCMXCIII in <URL:;; Abigail wrote:
;; > _
;; > Samik R. ([email protected]) wrote on VCCCLXIII September
:: > :) MCMXCIII in ;; > <URL:;; > :) Hello,
;; > :) I use the following regular expression to catch typical
:: > :) invalid email ;; > :) addresses:
;; > :) ------------
;; > :) my @Email=("sam._\@abc.org", "sam_.\@abc.org",
;; > :) "sam_.\@abc.org"); ;; > :) foreach (@Email)
;; > :) {
;; > :)
;; > :)
;; > :)
if(/^[A-z0-9]+([_\.][A-z0-9\-]+)*[@][A-z0-9_\-]+([.][A-z0-9_\-]+)?\.[A-z]{2,3}$/)
;; > :) { print "$_ is a valid email id\n"; } ;; > :) else
;; > :) { print "$_ is an invalid email id\n"; }
;; > :) }
;; > :) -------------
;; > :)
;; > :) This expression does not catch the above 3 emails in the
;; > :) array (the ;; > :) program says that they are valid emails).
;; > :)
;; > :) Can someone help me to discard these three?
;; >
;; > Sure.
;; >
;; > if (/_/) {
;; > print "$_ is invalid";
;; > }
;; > else {
;; > print "$_ is valid";
;; > }
;;
;; An e-mail address with _ (or ._ or _.) isn't invalid, AFAIK.


But that wasn't what he asked.

He wanted to filter out three particular email addresses.

Oh please, you know as well as most everyone else those were just
samples. In any case, flagging an email as invalid for having an
underscore ( /_/ ) is plain wrong, and portraying it as a solution is
disingenuous to the OP as well as anyone who comes by this in the
archives.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top