Email address regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig Bailey

    Email address regular expression

    I've been using this pattern to verify email addresses:

    ^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)

    But I've recently discovered that addresses with a dash in them won't
    pass this test.

    Seems to me they should pass.

    What's wrong?

    --
    Floydian Slip(tm) - "Broadcasti ng from the dark side of the moon"
    Random Precision Productions(tm)
    67 Union St. #2D, Winooski, Vt. 05404-1948 USA
    Sundays, 7-8 pm - Champ 101.3 FM, Colchester; 102.1 FM, Randolph, Vt.
    ccb@floydiansli p.com - AIM: RandomPrec - www.floydianslip.com
  • 127.0.0.1

    #2
    Re: Email address regular expression

    Craig Bailey wrote:
    [color=blue]
    > I've been using this pattern to verify email addresses:
    >
    > ^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)
    >
    > But I've recently discovered that addresses with a dash in them won't
    > pass this test.
    >
    > Seems to me they should pass.[/color]

    [A-Z] = All characters from A to Z, maybe a \- might help ?

    --
    Spam:newsgroup( at)craznar.com@ verisign-sux-klj.com
    EMail:<01100011 001011100110001 001110101011100 10011010110
    110010101000000 011000110111001 001100001011110 10011011100
    110000101110010 001011100110001 101101111011011 0100100000>

    Comment

    • Craig Bailey

      #3
      Re: Email address regular expression

      In article <Qpbgb.138702$b o1.108744@news-server.bigpond. net.au>,
      "127.0.0.1" <newsgroup(at)c raznar.com@veri sign-sux-ijlkl.com> wrote:
      [color=blue]
      > Craig Bailey wrote:
      >[color=green]
      > > I've been using this pattern to verify email addresses:
      > >
      > > ^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)
      > >
      > > But I've recently discovered that addresses with a dash in them won't
      > > pass this test.
      > >
      > > Seems to me they should pass.[/color]
      >
      > [A-Z] = All characters from A to Z, maybe a \- might help ?[/color]

      Aaaahhhh. But a dash doesn't need to be escaped, do it? Are you
      suggesting a add a dash to the first part of the expression? Like this?

      ^([0-9a-z-]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)

      Of course, that would allow someone to start their address with a dash,
      which I'm not certain is allowable in real email addresses.

      --
      Floydian Slip(tm) - "Broadcasti ng from the dark side of the moon"
      Random Precision Productions(tm)
      67 Union St. #2D, Winooski, Vt. 05404-1948 USA
      Sundays, 7-8 pm - Champ 101.3 FM, Colchester; 102.1 FM, Randolph, Vt.
      ccb@floydiansli p.com - AIM: RandomPrec - www.floydianslip.com

      Comment

      • Pedro

        #4
        Re: Email address regular expression

        [ not posted to alt groups ]

        Craig Bailey wrote:[color=blue]
        > I've been using this pattern to verify email addresses:
        >
        > ^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)
        >
        > But I've recently discovered that addresses with a dash in them won't
        > pass this test.
        >
        > Seems to me they should pass.
        >
        > What's wrong?[/color]

        Doesn't [0-9a-z\.-_] mean: 0 to 9 and a to z and dot to underscore?

        try [0-9a-z\._-]


        HTH

        --
        I have a spam filter working.
        To mail me include "urkxvq" (with or without the quotes)
        in the subject line, or your mail will be ruthlessly discarded.

        Comment

        • Manuel Lemos

          #5
          Re: Email address regular expression

          Hello,

          On 10/06/2003 07:16 AM, Craig Bailey wrote:[color=blue]
          > I've been using this pattern to verify email addresses:
          >
          > ^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)
          >
          > But I've recently discovered that addresses with a dash in them won't
          > pass this test.
          >
          > Seems to me they should pass.
          >
          > What's wrong?[/color]

          Character ranges that include - must start with that character. Take a
          look here at this e-mail address validation class that comes with a
          suitable regular expression besides the server based e-mail verification
          methods.




          --

          Regards,
          Manuel Lemos

          Free ready to use OOP components written in PHP
          Free PHP Classes and Objects 2026 Versions with PHP Example Scripts, PHP Tutorials, Download PHP Scripts, PHP articles, Remote PHP Jobs, Hire PHP Developers, PHP Book Reviews, PHP Language OOP Materials


          Comment

          • John Dunlop

            #6
            Re: Email address regular expression

            Pedro wrote:
            [color=blue]
            > Doesn't [0-9a-z\.-_] mean: 0 to 9 and a to z and dot to underscore?[/color]

            Yep. That's dot to underscore in "ASCII collating sequence". "Dot
            to underscore" matches fifty characters in total, including all
            uppercase letters, numbers, and a bunch of others -- but not the
            hyphen!
            [color=blue]
            > try [0-9a-z\._-][/color]

            Or even [\w.-]. ;-)

            --
            Jock

            Comment

            • John Dunlop

              #7
              Re: Email address regular expression

              Craig Bailey wrote:
              [color=blue]
              > Aaaahhhh. But a dash doesn't need to be escaped, do it?[/color]

              Not necessarily. But in its current position, yes, because it's
              indicating a range. The *hyphen* may only be left unescaped "where
              it cannot be interpreted as indicating a range".
              [color=blue]
              > Of course, that would allow someone to start their address with a dash,
              > which I'm not certain is allowable in real email addresses.[/color]

              I am certain it is.

              See RFC 2822 for details, especially section 3.4.1.

              --
              Jock

              Comment

              • Andy Hassall

                #8
                Re: Email address regular expression

                On Mon, 06 Oct 2003 11:56:14 -0300, Manuel Lemos <mlemos@acm.org > wrote:
                [color=blue]
                >Character ranges that include - must start with that character.[/color]

                Or end with it.

                --
                Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
                Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

                Comment

                • Pedro

                  #9
                  Re: Email address regular expression

                  John Dunlop wrote:[color=blue]
                  > Or even [\w.-]. ;-)[/color]

                  that will match (in some locales) "josécuña"

                  ps. hope that gets out ok, in HTML I'd write it as
                  jos&eacute;cu&n tilde;a

                  --
                  I have a spam filter working.
                  To mail me include "urkxvq" (with or without the quotes)
                  in the subject line, or your mail will be ruthlessly discarded.

                  Comment

                  • John Dunlop

                    #10
                    Re: Email address regular expression

                    Pedro wrote:
                    [color=blue]
                    > John Dunlop wrote:
                    >[color=green]
                    > > Or even [\w.-]. ;-)[/color]
                    >
                    > that will match (in some locales) "josécuña"[/color]

                    Ah. Well spotted! I forgot what the intended use was, and
                    concentrated on the one part. Thanks.
                    [color=blue]
                    > ps. hope that gets out ok[/color]

                    It did.

                    --
                    Jock

                    Comment

                    • Lothar Scholz

                      #11
                      Re: Email address regular expression - Never use them

                      Craig Bailey <ccb@floydiansl ip.com> wrote in message news:<ccb-CE3EE0.06162706 102003@news.ver izon.net>...[color=blue]
                      > I've been using this pattern to verify email addresses:
                      >
                      > ^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)
                      >
                      > But I've recently discovered that addresses with a dash in them won't
                      > pass this test.
                      >
                      > Seems to me they should pass.
                      >
                      > What's wrong?[/color]

                      In the book "Mastering Regular Expressions" there is an approximation
                      for internet email addresses, emails are to complex to be handed by
                      regular expressions correctly.

                      This regular expression is about 4800 characters long.

                      The best way is to not check any email addresses. Maybe assuming that
                      there must be a '@' is the only thing. Escpecially now where UTF can
                      be used in domain names, every [a-z] kind of pattern is simply wrong.

                      Comment

                      • William Maddler

                        #12
                        Re: Email address regular expression - Never use them

                        Lothar Scholz wrote:
                        [color=blue]
                        > In the book "Mastering Regular Expressions" there is an approximation
                        > for internet email addresses, emails are to complex to be handed by
                        > regular expressions correctly.[/color]

                        I agree...
                        usually what I check is to have smth before the @ and at least a word after
                        and that the mail address ends with a valid TLD...

                        --
                        WM

                        Comment

                        Working...