Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams Something to consider, doing ^(|.*@.*\..*)$ should have the same effect as it says either blank or this with only using ^ and $ once, to tidy it up a bit. Runevault Feb 24, 2012 at 18:20 This is pretty old, but I just stumbled across this and had trouble with the answer. There are cases where the beginning of a string is hidden but is still matched by ^ , where effectively you're looking for an email or nothing in the middle of a string. For this (email_regex)? is better-suited. jclancy Jun 18, 2013 at 23:26 Make sure to read up on how extremely complicated email validation is, before trying to use RegEx to do it. stackoverflow.com/questions/201323/… bryan kennedy Sep 24, 2014 at 17:20 This will match the following email test.test@test. This one is better I think ^$|^[^\s@]+@[^\s@]+\.[^\s@]+$ as it will accept emails like test@test.se dont_trust_me Dec 1, 2017 at 9:39 In a particular Java application, the ^$ doesn't work, but ^(?!.) does: [ ^ start of string, (?!) negative lookahead, . any character - not including linefeed] LightCC Jan 31, 2019 at 18:08

matching empty string or email

(^$|^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.(?:[a-zA-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$)

matching empty string or email but also matching any amount of whitespace

(^\s*$|^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.(?:[a-zA-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$)

see more about the email matching regex itself:

http://www.regular-expressions.info/email.html

The answers above work ($ for empty), but I just tried this and it also works to just leave empty like so:

/\A(INTENSE_EMAIL_REGEX|)\z/i

Same thing in reverse order

/\A(|INTENSE_EMAIL_REGEX)\z/i
                If you use ^\s*$ you don't need the or case. Also there is no need for g or i modifiers since this matches the whole line and doesn't involve any characters with case.
– Cadoo
                Jul 29, 2014 at 17:19

If you need to cover any length of empty spaces then you may want to use following regex:

"^\s*$"

Don't match an email with a regex. It's extremely ugly and long and complicated and your regex parser probably can't handle it anyway. Try to find a library routine for matching them. If you only want to solve the practical problem of matching an email address (that is, if you want wrong code that happens to (usually) work), use the regular-expressions.info link someone else submitted.

As for the empty string, ^$ is mentioned by multiple people and will work fine.

Ugly regex can in fact be handled by regex parsers. Not using a regex just because it's ugly is silly. – Peter Jul 10, 2013 at 18:05 This isn't constructive. RegEx is tried and tested (and far from ugly -- would you say it's quite elegant how it works?). To vaguely suggest a library without any guidance on a possible solution defeats the purpose of responding at all. Everyone needs to bookmark: code.tutsplus.com/tutorials/… – pim Oct 17, 2014 at 19:24 For the record: I would gladly recommend a specific library if I knew what language OP was using. Since this is a language-agnostic question, my options are rather limited. – Kevin Mar 24, 2015 at 18:46 Regex isn't supposed to be pretty. By that logic, nobody should ever use regex. If you're so adamant about a library replacing it, what then do you think that library is going to do? It's either going to have very long, complicated code, or it's going to use regex... By that point you might as well just implement your own "library". And nowadays, if somebody doesn't know how to use regex, they probably shouldn't be tasked with validating email addresses anyways... – Andrew Mar 28, 2017 at 13:53 @Kevin Also, no, it definitely does not precisely mean the same thing, because of its context: "Don't match an email with a regex" means, "Don't use regex." "Don't match an email with a single regex" means, "One regex won't cut it." – Andrew Jan 10, 2018 at 17:59

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.