190

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.

0

8 Answers 8

433

This regex pattern will match an empty string:

^$

And this will match (crudely) an email or an empty string:

(^$|^.*@.*\..*$)
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
Make sure to read up on how extremely complicated email validation is, before trying to use RegEx to do it. stackoverflow.com/questions/201323/…
This will match the following email test.test@test. This one is better I think ^$|^[^\s@]+@[^\s@]+\.[^\s@]+$ as it will accept emails like [email protected]
In a particular Java application, the ^$ doesn't work, but ^(?!.) does: [^ start of string, (?!) negative lookahead, . any character - not including linefeed]
10

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

Comments

3

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

Comments

3

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

"^\s*$"

Comments

2

this will solve, it will accept empty string or exact an email id

"^$|^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"

1 Comment

could you be a little more descriptive.
2

I prefer /^\s+$|^$/gi to match empty and empty spaces.

console.log("  ".match(/^\s+$|^$/gi));
console.log("".match(/^\s+$|^$/gi));

1 Comment

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.
0

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 }

Comments

-3

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.

10 Comments

Ugly regex can in fact be handled by regex parsers. Not using a regex just because it's ugly is silly.
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/…
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.
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...
@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."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.