Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. best regex mail pattern ?

best regex mail pattern ?

Scheduled Pinned Locked Moved C#
regexcsharpalgorithmsquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cemlouis
    wrote on last edited by
    #1

    Hi, I am searching for the best regex mail pattern with C#. Thank you, Cem Louis

    B H 2 Replies Last reply
    0
    • C cemlouis

      Hi, I am searching for the best regex mail pattern with C#. Thank you, Cem Louis

      B Offline
      B Offline
      Brian Nottingham
      wrote on last edited by
      #2

      private Regex emailReg = new Regex( @"(?\w[-.\w]*)@(?[-a-z0-9]+(\.[-a-z0-9]+)*\." + @"(com|edu|gov|int|mil|net|org|biz|info|name|museum|coop|aero|[a-z][a-z]))", RegexOptions.IgnoreCase); Credit goes to http://www.bookpool.com/.x/ocstn6vf4n/sm/0596002890[^]

      1 Reply Last reply
      0
      • C cemlouis

        Hi, I am searching for the best regex mail pattern with C#. Thank you, Cem Louis

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        I don't recommend hard-coding the TLD (top-level domains). Change that to ([a-z]{1,}) instead and let DNS do its job. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

        B 1 Reply Last reply
        0
        • H Heath Stewart

          I don't recommend hard-coding the TLD (top-level domains). Change that to ([a-z]{1,}) instead and let DNS do its job. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

          B Offline
          B Offline
          Brian Nottingham
          wrote on last edited by
          #4

          Following that logic, you wouldn't need a Regex at all. Just try to send the email, using any random string as the address, and "let DNS do its job". ([a-z]{1,}) validates lots of invalid email addresses. The last time I checked, only the TLD's, plus 2 letter combinations were valid. Its best to spell this out explicitly in the regex.

          L H 2 Replies Last reply
          0
          • B Brian Nottingham

            Following that logic, you wouldn't need a Regex at all. Just try to send the email, using any random string as the address, and "let DNS do its job". ([a-z]{1,}) validates lots of invalid email addresses. The last time I checked, only the TLD's, plus 2 letter combinations were valid. Its best to spell this out explicitly in the regex.

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            Brian Nottingham wrote: "let DNS do its job". I think Heath meant actually checking your DNS server for a valid MX record. :) And not blindly sending mail... top secret
            Download xacc-ide 0.0.3 now!
            See some screenshots

            1 Reply Last reply
            0
            • B Brian Nottingham

              Following that logic, you wouldn't need a Regex at all. Just try to send the email, using any random string as the address, and "let DNS do its job". ([a-z]{1,}) validates lots of invalid email addresses. The last time I checked, only the TLD's, plus 2 letter combinations were valid. Its best to spell this out explicitly in the regex.

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              So, by your token it's good to hard-code that which changes often. What leppie said is right, and at the very least if you're going to hard-code these values read them from a .config file instead (like from <appSettings> or your own configuration section. Should one honestly have to fix, recompile, and re-deploy the assemblies that use this each time a new TLD is added? This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

              B 1 Reply Last reply
              0
              • H Heath Stewart

                So, by your token it's good to hard-code that which changes often. What leppie said is right, and at the very least if you're going to hard-code these values read them from a .config file instead (like from <appSettings> or your own configuration section. Should one honestly have to fix, recompile, and re-deploy the assemblies that use this each time a new TLD is added? This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

                B Offline
                B Offline
                Brian Nottingham
                wrote on last edited by
                #7

                I agree with you, that hard-coding those vales could make it hard to maintain and update the application. Depending on the intended use, there are always tradeoffs. Read from a config file, hard-code them, use your ([a-z]{1,}) method, and probably a few others. The goal of my original post was to give a good starting point for an email regex, and I considered the example from Jeffrey Friedl's book to be pretty good. Thank you for pointing out ways in which it could be improved in practice.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups