Regular expression question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    Regular expression question

    /^[a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*@[a-zA-Z_-]+([.][a-zA-Z0-9_-]+)*$/

    I'm using this to try to validate a small subset of the valid e-mail
    addresses allowed by the relevant RFC (alphanumerics, underscores, and
    dashes). I've tested it and it seems to work - I'm just looking for
    helpful hints :)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • Lasse Reichstein Nielsen

    #2
    Re: Regular expression question

    Christopher Benson-Manica <ataru@nospam.c yberspace.org> writes:
    [color=blue]
    > /^[a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*@[a-zA-Z_-]+([.][a-zA-Z0-9_-]+)*$/
    >
    > I'm using this to try to validate a small subset of the valid e-mail
    > addresses allowed by the relevant RFC (alphanumerics, underscores, and
    > dashes). I've tested it and it seems to work - I'm just looking for
    > helpful hints :)[/color]

    Well, one hint would be to not take such a restrictive subset. :)

    The range [a-zA-Z0-9_] can be written as \w, and [.] and \. are
    equivalent, so a shorter version is:

    /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/

    (I changed the last "*" to "+", because all domain names must have at
    least one ".").

    Again, I can't see what real purpose such a restrictive subset can
    be used for. :)
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Michael Winter

      #3
      Re: Regular expression question

      Christopher Benson-Manica wrote:
      [color=blue]
      > /^[a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*@[a-zA-Z_-]+([.][a-zA-Z0-9_-]+)*$/[/color]

      Rather than the character class

      [a-zA-Z0-9_-]

      you can use

      [\w-]

      I'd also escape the dots with a backslash, rather than specifying a
      character class.

      Making the last group optional is debatable. Whilst I believe it's
      valid (an address could be a single string if it aliases another
      destination), it's not of much value on the Web as all domains will
      have at least one dot.

      Out of interest, did you intend to make the two character classes
      after the @ different, or is one of them a typo?

      [snip]

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Christopher Benson-Manica

        #4
        Re: Regular expression question

        Michael Winter <m.winter@bluey onder.co.invali d> spoke thus:
        [color=blue]
        > [\w-][/color]

        I wasn't aware you could do that - I will. Thanks.
        [color=blue]
        > Out of interest, did you intend to make the two character classes
        > after the @ different, or is one of them a typo?[/color]

        It was a typo, which I suppose further argues in favor of the
        abbreviated version you presented above :)

        --
        Christopher Benson-Manica | I *should* know what I'm talking about - if I
        ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

        Comment

        • Christopher Benson-Manica

          #5
          [OT: was Re: Regular expression question]

          Lasse Reichstein Nielsen <lrn@hotpop.com > spoke thus:
          [color=blue]
          > Again, I can't see what real purpose such a restrictive subset can
          > be used for. :)[/color]

          Do any real e-mail addresses use the other allowable characters? We
          just wanted something fairly simple without characters that might
          cause problems elsewhere.

          --
          Christopher Benson-Manica | I *should* know what I'm talking about - if I
          ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: [OT: was Re: Regular expression question]

            Christopher Benson-Manica <ataru@nospam.c yberspace.org> writes:
            [color=blue]
            > Do any real e-mail addresses use the other allowable characters? We
            > just wanted something fairly simple without characters that might
            > cause problems elsewhere.[/color]

            I regularly *try* to enter something on the form
            name+extrainfo@ example.com
            That allows me to quickly create a unique address for each recipient,
            so I can see who gives my addres to spammers (and allows me to block it),
            all using only one mailbox.

            However "cause problems elsewhere" is so vague, that perhaps you
            should decide what programs the address must interact with, and what
            their requirements are. After all, you probably don't want to turn
            somebody away, just because their e-mail address is not matching
            the lowest common denominator, unless you *really* need to.

            (but no, I don't usually see such addresses)
            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Christopher Benson-Manica

              #7
              Re: [OT: was Re: Regular expression question]

              Lasse Reichstein Nielsen <lrn@hotpop.com > spoke thus:
              [color=blue]
              > I regularly *try* to enter something on the form
              > name+extrainfo@ example.com
              > That allows me to quickly create a unique address for each recipient,
              > so I can see who gives my addres to spammers (and allows me to block it),
              > all using only one mailbox.[/color]

              Hm, I see. I'm fairly certain that we're not a spam entity though :)
              [color=blue]
              > However "cause problems elsewhere" is so vague, that perhaps you
              > should decide what programs the address must interact with, and what
              > their requirements are. After all, you probably don't want to turn
              > somebody away, just because their e-mail address is not matching
              > the lowest common denominator, unless you *really* need to.[/color]

              Well, I suppose if we get complaints, we can URIEncode the form fields
              and allow some more of the characters, but that doesn't sound too
              likely.

              --
              Christopher Benson-Manica | I *should* know what I'm talking about - if I
              ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

              Comment

              Working...