Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I know there's a few regexes floating around to help detect email addresses. I hear there's also one that's like 5000-6000 characters.

Can someone give me a fairly workable email regex with an example of how to pull back an email address from a $content variable with HTML coming from a given page?

use LWP::Simple; my $content = get($url); my @emails = $content =~ m//g;

Replies are listed 'Best First'.
Re: An email regex
by chromatic (Archbishop) on Apr 06, 2007 at 20:53 UTC
Re: An email regex
by kyle (Abbot) on Apr 06, 2007 at 19:54 UTC
Re: An email regex
by barvin (Beadle) on Apr 06, 2007 at 20:07 UTC
    Have a look at Regexp::Common::Email::Address on CPAN
      The use of Regexp::Common can be a bit confusing if you have not used it before. Here is a snippet that may help:
      use Regexp::Common qw(Email::Address); ## and later if ($data =~ $RE{Email}{Address}{-keep}) { $email = $1; }
      Cheers.
Re: An email regex
by Herkum (Parson) on Apr 07, 2007 at 03:44 UTC

    While there a few options for validating an email address you always have this problem. There is no way to validate an address is valid without sending an email.

    Basically, don't worry too much about validating the email address because even if it is valid format, it does not mean that it is a valid email account.

Re: An email regex
by bradcathey (Prior) on Apr 07, 2007 at 14:38 UTC

    I use Email::Valid with good results.

    use Email::Valid; use CGI qw/:standard/; if ( !Email::Valid->address( $query->param('email') ) { print "E-mail address is not valid or is blank"; }

    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: An email regex
by blazar (Canon) on Apr 07, 2007 at 10:38 UTC
    Can someone give me a fairly workable email regex with an example of how to pull back an email address from a $content variable with HTML coming from a given page?

    Given the emphasis on "workable" above, not really/completely on topic (yet!) but... already written about that.