from Hacker News

Email Address Regular Expression That 99.99% Works

by arun-mani-j on 10/24/23, 9:27 AM with 37 comments

  • by Etheryte on 10/24/23, 10:07 AM

    Yeah no, if you try to check this into any codebase it should get rejected straight away. There is no need to regex emails pretty much anywhere, when you use an email for signup or similar you send a confirmation email which serves the same purpose - to make sure the address is valid and correct. Use `<input type="email">` or check that "@" is present if you must, but anything beyond that is nonsense.
  • by tiborsaas on 10/24/23, 10:07 AM

    Don't use a regex, test only for a @ and a dot after it and call it a day.

    Btw content is unreadable thanks to aggressive adblocker detection so much that this feels like spam.

  • by evrimoztamur on 10/24/23, 10:05 AM

    Each time that this or its variants get posted here, the response is the same:

    Check if there’s an @ and send a verification email.

  • by Retr0id on 10/24/23, 10:05 AM

  • by a9ex on 10/24/23, 10:12 AM

    I usually just test for the presence of @ - that’s it. Works for IDN addresses as well.

    I remember some story back in the day where someone had an email address using the top level to domain only. Like “x@to”, pretty cool but probably a pain to use (:

  • by creshal on 10/24/23, 10:16 AM

    Why does this site need an adblocker-blocker?
  • by arp242 on 10/24/23, 10:16 AM

    Use "mail.parse()" or whatever there is for your language. In general just parse stuff instead of letting some regexp loose on it.

    For a quick check to make sure people don't mix up fields or accidentally hit enter before they finished typing, just /.+@.+\..{2,}/ is more than enough (technically foo@com is valid, but no one uses that – note that root@localhost or cron@sysops CAN be valid, so in some contexts you want to use just /.+@.+/, but that doesn't really apply for signups and the like).

  • by mmh0000 on 10/24/23, 2:56 PM

    This[1] is or at least, was PHPs validator for email. Author provides a lot of insight into the complexity of regexing emails.

    [1] https://fightingforalostcause.net/content/misc/2006/compare-...

  • by arun-mani-j on 10/24/23, 11:35 AM

    I have uBlock Origin and it works fine in my Firefox.

    Sorry I didn't knew of this adblock detection nonsense, otherwise wouldn't have posted it. :(

  • by MilStdJunkie on 10/24/23, 5:44 PM

    Why would you do this?! Mail parse libraries are everywhere
  • by sam_lowry_ on 10/24/23, 1:31 PM

    The first thing I tried, root@localhost, did not match ;/