best regex mail pattern ?
-
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[^] -
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] -
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]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.
-
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.
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 -
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.
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] -
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]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.