Regex for checking Email format?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • unndunn@gmail.com

    Regex for checking Email format?

    I'm trying to design a regular expression that matches (using
    preg_match()) when a string is a well-formed Email address.

    So far I have this: /^[A-Z0-9._%\-]+@[A-Z0-9._%\-]+\.[A-Z]{2,4}$/i
    I got that from reguar-expressions.inf o. But PHP keeps complaining of
    "Unknown modifier 'Z'".

    For the life of me I can't figure out how 'Z' is a modifier.
    Can anyone help? Thanks much.

  • Pedro Graca

    #2
    Re: Regex for checking Email format?

    unndunn@gmail.c om wrote:[color=blue]
    > I'm trying to design a regular expression that matches (using
    > preg_match()) when a string is a well-formed Email address.
    >
    > So far I have this: /^[A-Z0-9._%\-]+@[A-Z0-9._%\-]+\.[A-Z]{2,4}$/i
    > I got that from reguar-expressions.inf o. But PHP keeps complaining of
    > "Unknown modifier 'Z'".
    >
    > For the life of me I can't figure out how 'Z' is a modifier.
    > Can anyone help? Thanks much.[/color]

    My PHP does not complain.

    php$ php --version
    PHP 4.3.9-1 (cli) (built: Oct 5 2004 08:45:32)
    Copyright (c) 1997-2004 The PHP Group
    Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

    php$ cat preg.php
    <?php

    $regexp = '/^[A-Z0-9._%\-]+@[A-Z0-9._%\-]+\.[A-Z]{2,4}$/i';
    $email = 'hexkid@dodgeit .com';

    if (preg_match($re gexp, $email)) {
    echo 'Match!';
    }
    echo "\n";

    ?>

    php$ php preg.php
    Match!



    Also see


    This is a Perl regexp, it should work with PHP unchanged (never tried it).
    --
    Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
    == ** ## !! !! ## ** ==
    TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
    bypass the spam filter. I will answer all pertinent mails from a valid address.

    Comment

    • Michael Fesser

      #3
      Re: Regex for checking Email format?

      .oO(unndunn@gma il.com)
      [color=blue]
      >I'm trying to design a regular expression that matches (using
      >preg_match() ) when a string is a well-formed Email address.
      >
      >So far I have this: /^[A-Z0-9._%\-]+@[A-Z0-9._%\-]+\.[A-Z]{2,4}$/i
      >I got that from reguar-expressions.inf o. But PHP keeps complaining of
      >"Unknown modifier 'Z'".[/color]

      The regex works here, but there's a more general problem.

      The syntax of an e-mail address can be rather complex, there are some
      different formats described in RFC 822. It's no easy task (if possible
      at all) to check a mail address for validity just with simple regular
      expressions.

      The regex above for example still allows invalid addresses (like
      foo@1..bar), while keeping valid ones out (like foo!@example.co m,
      foo@example.mus eum).

      You might want to check out <http://www.phpclasses. org/emailvalidation >,
      but even this class is not perfect.

      Micha

      Comment

      • W47

        #4
        Re: Regex for checking Email format?

        Pedro Graca wrote:
        [color=blue]
        > http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html[/color]

        Grand as it may be, that pattern does not cater for comments
        in addresses.* The module can however replace any with
        whitespace before the pattern is applied. Anyway, as its
        description says, 'the only sure way to see if a supplied
        email address is genuine is to send an email to it and see
        if the user recieves [sic] it'.


        * See my From header. Though Gravity2.60 allows me to put
        comments in my address, it doesn't cope well: entering a
        display name and an address with comments results in a
        malformed From header.

        Cheers!

        --
        Jock

        Comment

        • unndunn@gmail.com

          #5
          Re: Regex for checking Email format?

          Thank you very much. For some reason your logic worked, and mind
          didn't. I still can't figure out why, because I have the exact same
          regex and I had nearly identical logic.
          Meh, no matter. It works now. Thanks again.

          Comment

          • unndunn@gmail.com

            #6
            Re: Regex for checking Email format?

            Thanks for the heads up... but this script is really a temporary thing.
            I'm not aiming for bulletproof email validation, just something that is
            good enough to keep the obviously-bogus addresses out. For applications
            where I need bulletproof email validation, I usually send a
            confirmation message.

            Comment

            • John Dunlop

              #7
              Re: Regex for checking Email format?

              unndunn@gmail.c om wrote:
              [color=blue]
              > I'm not aiming for bulletproof email validation, just something that is
              > good enough to keep the obviously-bogus addresses out.[/color]

              So why not just check for the presence of a local part, a
              '@' and a host name? Or even just an '@'? As Michael has
              said, your pattern already allows obviously-bogus addresses
              while disallowing perfectly reasonable ones.
              [color=blue]
              > For applications where I need bulletproof email validation, I usually
              > send a confirmation message.[/color]

              Good.

              --
              Jock

              Comment

              • unndunn@gmail.com

                #8
                Re: Regex for checking Email format?

                OK, now I'm totally confused. I guess the best way to do it would be to
                check for an '@' and try to determine if the hostname resolves to a
                server...

                Comment

                • Tim Van Wassenhove

                  #9
                  Re: Regex for checking Email format?

                  In article <1100731752.898 575.325740@f14g 2000cwb.googleg roups.com>, unndunn@gmail.c om wrote:[color=blue]
                  > OK, now I'm totally confused. I guess the best way to do it would be to
                  > check for an '@' and try to determine if the hostname resolves to a
                  > server...[/color]

                  And what if there are 2 @ in the address?


                  --
                  Met vriendelijke groeten,
                  Tim Van Wassenhove <http://www.timvw.info>

                  Comment

                  Working...