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

Can someone please suggest a regular expression that will pick out a valid e-mail address? My web application form keeps getting bounced by our mailserver...

Replies are listed 'Best First'.
Re: E-mail Regular Expression
by kschwab (Vicar) on Jul 28, 2001 at 20:17 UTC
    Finding a valid email address with a regex is more complicated than you would think. Fortunately, there's Email::Valid.
Re: E-mail Regular Expression
by azatoth (Curate) on Jul 28, 2001 at 20:18 UTC
Re: E-mail Regular Expression
by rob_au (Abbot) on Jul 29, 2001 at 07:45 UTC
    One additional point to consider ... while Email::Valid as pointed out by kschwab and azatoth is by far the better method by which to validate email addresses (compared to a single regex), this will not negate the possibility of bounced mail coming back to your mail server - This moduile performs a check to see if the email address appears to be valid, including RFC822 checks and DNS MX record checks, but does not guarentee the validity of the email address with regards to delivery. It is still quite possible for someone to enter a 'bogus' email address that passes RFC and DNS checks, but cannot me delivered, whether due to mailer or remote configuration errors. eg. quota limits, unknown user, etc.
     
    This is something to keep in mind where sending mail from web applications and can be curtailed through judicious use of Email::Valid (with mxcheck enabled) and use of valid From: header fields in your application to direct bounced mail (on one of my boxes I have a mailbox directed to /dev/null specifically for such bounce messages).
     
    Update: I should also point out that this aspect of mail delivery and handling of bounced mail is true of ANY email validation system (short of the successful delivery and receipt of a test email message) - Email::Valid however offers the most comprehensive and complete means (that I know of) of validating an email address prior to mail delivery.
     

     
    Ooohhh, Rob no beer function well without!
Re: E-mail Regular Expression
by miyagawa (Chaplain) on Jul 29, 2001 at 21:39 UTC
    There are also Mail::CheckUser or Mail::Verify, which can try to check validity on MX (VRFY, RCPT etc.) against Email addresses, rather than RFC822 syntax check that Email::Valid does.

    --
    Tatsuhiko Miyagawa
    miyagawa@cpan.org

      It is worth noting that some servers don't support VRFY; some will be acting as secondary mail servers so may accept all RCPTs for their domains.

      There are a whole bunch of other conditions; using Email::valid with mxchecking is going to reduce bad addresses by a great deal -- I often find it's easier to use my real address than to try and think of a valid email address...

      --
      RatArsed

        You're right. In fact, using VRFY sometimes may be taken for network abuse. Thus, I haven't ever used these modules ... Email::Valid will do :-)

        --
        Tatsuhiko Miyagawa
        miyagawa@cpan.org

Re: E-mail Regular Expression
by fx (Pilgrim) on Jul 29, 2001 at 22:37 UTC

    The above replies offer good advice as to how to check an obtained e-mail address is valid, but I believe the question was about a regular expression which could pull an e-mail address from a string of text (which you would then, of course, feed to validating code).

    I had to do this once at work and knocked up a quick regexp for an e-mail address. I think it's extremely messy, but it seemed to work in all test cases and also in production. (I think it's messy because I made sure words started and ended with only a letter or number, but the middle of words could contain things such as dashes, underscores, single quotes, or what ever was applicable.)

    I used: $string =~ /([A-Z0-9]+[A-Z_.'0-9-]*[A-Z0-9]{1}\@{1}(?:[A-Z0-9]+[A-Z0-9-]*[A-Z0-9]{1}\.)+[A-Z0-9]+[A-Z0-9-]*[A-Z0-9]{1})/i; to place the e-mail address in $1.