java Java中电子邮件验证的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2762977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:42:34  来源:igfitidea点击:

regular expression for email validation in Java

javaregexemail-validation

提问by aks

I am using the follwoing regular expression

我正在使用以下正则表达式

(".+@.+\.[a-z]+")

Bit it accepts #@#.com as a valid email. What's the pattern I should use?

它接受#@#.com 作为有效电子邮件的位。我应该使用什么模式?

回答by CoolBeans

You should use apache-commons email validator. You can get the jar file from here.

您应该使用 apache-commons 电子邮件验证器。您可以从这里获取 jar 文件。

Here is a simple example of how to use it:

这是一个如何使用它的简单示例:

import org.apache.commons.validator.routines.EmailValidator;

boolean isValidEmail = EmailValidator.getInstance().isValid(emailAddress);

回答by David Z

Here's a web page that explains that better than I can: http://www.regular-expressions.info/email.html(EDIT: that appears to be a bit out of date since it refers to RFC 2822, which has been superseded by RFC 5322)

这是一个比我能更好地解释的网页:http: //www.regular-expressions.info/email.html编辑:这似乎有点过时,因为它指的是 RFC 2822,已被取代通过 RFC 5322)

And another with an interesting take on the problem of validation: http://www.markussipila.info/pub/emailvalidator.php

另一个对验证问题有一个有趣的看法:http: //www.markussipila.info/pub/emailvalidator.php

Generally the best strategy for validating an email address is to just try sending mail to it.

通常,验证电子邮件地址的最佳策略是尝试向其发送邮件。

回答by Konstantin Spirin

If somebody wants to enter non-existent email address he'll do it whatever format validation you choose.

如果有人想输入不存在的电子邮件地址,他会按照您选择的任何格式进行验证。

The only way to check that user owns email he entered is to send confirmation (or activation) link to that address and ask user to click it.

检查用户是否拥有他输入的电子邮件的唯一方法是向该地址发送确认(或激活)链接并要求用户单击它。

So don't try to make life of your users harder. Checking for presence of @is good enough.

所以不要试图让你的用户的生活变得更艰难。检查 的存在@就足够了。

回答by Mahdi Esmaeili

[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}

[A-Z0-9._%+-]+@[A-Z0-9.-]+.[AZ]{2,4}

回答by Nitin Pawar

import java.util.regex.*;

class ValidateEmailPhone{

    public static void main(String args[]){

        //phone no validation starts with 9 and of 10 digit
        System.out.println(Pattern.matches("[9]{1}[0-9]{9}", "9999999999"));

        //email validation
        System.out.println(Pattern.matches("[a-zA-Z0-9]{1,}[@]{1}[a-z]{5,}[.]{1}+[a-z]{3}", "[email protected]"));
    }
}

回答by sp00m

I usually use the following one:

我通常使用以下一种:

([a-zA-Z0-9]+(?:[._+-][a-zA-Z0-9]+)*)@([a-zA-Z0-9]+(?:[.-][a-zA-Z0-9]+)*[.][a-zA-Z]{2,})