Perl: Regular expression for email address

There are many options and variations available for creating regular expression for email address.

However I am mentioning 2 of them which can be used for general purpose.

The first regex will accept email address in any of the below format:

someone@example.com, some_one@example.com or some.one@example.com or some-one@example.com and so on.

The second regex will accept email address in only below 4 formats:

some_one@example.com, some.one@example.com, some+one@example.com,some-one@example.com

—————————————————————————-

#mail_address_validation.pl

print “\nEnter an email address:”;
my $mailadr = <STDIN>;

if ($mailadr =~ ((/^\S+@\S+.\S+$/) ))
{ print “\nValid email address.”;
}
else {print “\nNot cool”;}

if ($mailadr =~ (/^\w+[_.+-]\w+@\w+\.\w+$/))
{ print “\nValid email address”; }
else {print “\nNot cool”}

————————————————————————————

Advertisements

Tags: , , , ,

One Response to “Perl: Regular expression for email address”

  1. koolkedar Says:

    The output of the above script for various inputs is:

    Enter an email address:ad_bbc@example.com
    Valid email address.
    Valid email address.

    ——————————————————————-

    Enter an email address:abc@example.com
    Valid email address.
    Not cool

    ——————————————————————–

    Enter an email address:a.bc@example.com
    Valid email address.
    Valid email address.

    ———————————————————————–

    Enter an email address:abc
    Not cool
    Not cool

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: