I found a lot of Regex email validation in SO but I did not find any that will accept an empty string. Is this possible through Regex only? Accepting either empty string or email only? I want to have this on Regex only.
8 Answers
This regex pattern will match an empty string:
^$
And this will match (crudely) an email or an empty string:
(^$|^.*@.*\..*$)

- 7,573
- 2
- 25
- 37
-
1Something 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 '12 at 18:20
-
1This 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 '13 at 23:26
-
Make sure to read up on how extremely complicated email validation is, before trying to use RegEx to do it. http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – bryan kennedy Sep 24 '14 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 01 '17 at 09: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 '19 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:

- 2,928
- 2
- 21
- 28
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

- 967
- 2
- 15
- 28
I prefer /^\s+$|^$/gi
to match empty and empty spaces.
console.log(" ".match(/^\s+$|^$/gi));
console.log("".match(/^\s+$|^$/gi));

- 15,024
- 7
- 48
- 87

- 55,053
- 85
- 237
- 424
-
3If 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 '14 at 17:19
this will solve, it will accept empty string or exact an email id
"^$|^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"

- 49,085
- 60
- 166
- 233

- 21
- 1
If you need to cover any length of empty spaces then you may want to use following regex:
"^\s*$"

- 428
- 4
- 8
If you are using it within rails - activerecord validation you can set
allow_blank: true
As:
validates :email, allow_blank: true, format: { with: EMAIL_REGEX }

- 20,879
- 9
- 40
- 61

- 952
- 1
- 13
- 18
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.

- 28,963
- 9
- 62
- 81
-
18Ugly regex can in fact be handled by regex parsers. Not using a regex just because it's ugly is silly. – Peter Jul 10 '13 at 18:05
-
1This 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: http://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149 – pim Oct 17 '14 at 19:24
-
Regexes are notoriously hard to read, maintain, and debug, which makes them a prime candidate for replacement by a call to a library. – einnocent Mar 24 '15 at 17:02
-
2For 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 '15 at 18:46
-
2Regex **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 '17 at 13:53
-
@Andrew: That is a ridiculous false dichotomy. There are plenty of situations in which regexes are appropriate. Email is not one of them. Did you read the page I linked to? If your regex is anything *like* that complicated, it's too damn big. You should break it into smaller regexes with simple flow control (`if` statements, functions, etc.). At that point, yes, you are implementing your own library. I fail to see your point. – Kevin Mar 28 '17 at 19:28
-
So what you really meant then is "Don't match an email with a **single** regex." That would be an answer that would get you more than -1 votes. – Andrew Mar 28 '17 at 20:00
-
@Andrew: Now you're just splitting hairs. "A regex" means precisely the same thing as "a single regex." – Kevin Jan 10 '18 at 15:31
-
@Kevin You mentioned a false dichotomy earlier, yet your answer itself is a false dichotomy. Then when I clarify it you neglect the fact that you basically give two options: either use an extremely ugly and long and complicated regex, which "your regex parser probably can't handle" anyways, or find some library. Your answer is misleading and does not even leave room to suggest that combined regexes and code is a viable and good solution. – Andrew Jan 10 '18 at 17:58
-
1@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 '18 at 17:59