Java正则expression式电子邮件

首先,我知道使用正则expression式的电子邮件不build议,但我得testing一下。

我有这个正则expression式:

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

在Java中,我这样做了:

 Pattern p = Pattern.compile("\\b[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[AZ]{2,4}\\b"); Matcher m = p.matcher("foobar@gmail.com"); if (m.find()) System.out.println("Correct!"); 

但是,不pipe电子邮件是否形成良好,正则expression式都会失败。 Eclipse中的“查找和replace”工作正常的正则expression式。

任何想法?

谢谢,

FWIW,这里是我们用来validation电子邮件地址的Java代码。 正则expression式非常相似:

 public static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[AZ]{2,6}$", Pattern.CASE_INSENSITIVE); public static boolean validate(String emailStr) { Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(emailStr); return matcher.find(); } 

工作相当可靠。

这里是适用于Java的符合RFC822的regex:

 Pattern ptr = Pattern.compile("(?:(?:\\r\\n)?[ \\t])*(?:(?:(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\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^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*\\>(?:(?:\\r\\n)?[ \\t])*))*)?;\\s*)"); String[] emails = { "\"Fred Bloggs\"@example.com", "user@.invalid.com", "Chuck Norris <gmail@chucknorris.com>", "webmaster@müller.de", "matteo@78.47.122.114" }; for (String email : emails) { System.out.println(email + " is " + (ptr.matcher(email).matches() ? "valid" : "invalid")); } 

输出:

 "Fred Bloggs"@example.com is valid user@.invalid.com is invalid Chuck Norris <gmail@chucknorris.com> is valid webmaster@müller.de is valid matteo@78.47.122.114 is valid 

正则expression式是从这个职位: Mail :: RFC822 ::地址:基于regexp的地址validation 。 结果应该与在线版本一致。

别。 你将永远不会有一个有效的expression式。

例如,这些都是有效的电子邮件地址:

 "Abc\@def"@example.com "Fred Bloggs"@example.com "Joe\\Blow"@example.com "Abc@def"@example.com customer/department=shipping@examp le.com $A12345@example.com !def!xyz%abc@example.com _somename@example.com matteo(this is a comment).corti@example.com root@[127.0.0.1] 

只是提到一些问题:

  • 您不会考虑指定主机的多种forms(例如通过IP地址)
  • 你错过了有效的字符
  • 你错过了非ASCII的域名

甚至在开始之前检查相应的RFC

那是因为你忘记了不区分大小写:

 Pattern regex = Pattern.compile("\\b[\\w.%-]+@[-.\\w]+\\.[A-Za-z]{2,4}\\b"); 

这符合你的例子,尽pipe它忽略了许多有效的电子邮件。

加工是否设置为CASE_INSENSITIVE ?

另一个简单的select来validation99%的电子邮件

 public static final String EMAIL_VERIFICATION = "^([\\w-\\.]+){1,64}@([\\w&&[^_]]+){2,255}.[az]{2,}$"; 

您可以使用此方法validation在Java中的电子邮件地址。

 public class EmailValidator { private Pattern pattern; private Matcher matcher; private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; public EmailValidator() { pattern = Pattern.compile(EMAIL_PATTERN); } /** * Validate hex with regular expression * * @param hex * hex for validation * @return true valid hex, false invalid hex */ public boolean validate(final String hex) { matcher = pattern.matcher(hex); return matcher.matches(); } } 

通用电子邮件格式正则expression式: –

 String regexEmail = "^[A-Za-z](.*)([@]{1})(.{1,})(\\.)(.{1,})"; 

一般电子邮件格式(RE),其中还包括co.in,co.uk,com,outlook.com等域名。

规则说:

  • 大写和小写英文字母(az,AZ)
  • 数字0到9
  • 人物! #$%&'* + – / =? ^ _`{| }〜字符。
  • (点,句号,句号),前提是不是第一个或最后一个字符,并且规定它不连续出现两次或更多次。

     [a-zA-Z0-9]+[._a-zA-Z0-9!#$%&'*+-/=?^_`{|}~]*[a-zA-Z]*@[a-zA-Z0-9]{2,8}.[a-zA-Z.]{2,6} 

正则expression式: ^[\\w!#$%&'*+/=? {|}〜^ – ] +(?:\。[\ w!#$%&'* + / =? {|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$

 public static boolean isValidEmailId(String email) { String emailPattern = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"; Pattern p = Pattern.compile(emailPattern); Matcher m = p.matcher(email); return m.matches(); } 

如果你想允许非stream行的angular色,这个对我来说很好。

 "^[\\p{L}\\p{N}\\._%+-]+@[\\p{L}\\p{N}\\.\\-]+\\.[\\p{L}]{2,}$" 

它在@之后不允许IP,但是xxx@xxx.TDL最有效的电子邮件可以用它来validation。 \p{L}validationUTF-letters, \p{N}validationUTF-Numbers。 你可以查看这个文档以获取更多信息。

尝试这个

 String regex = "^[\\p{L}\\p{N}\\._%+-]+@[\\p{L}\\p{N}\\.\\-]+\\.[\\p{L}]{2,}$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(Email); if (matcher.matches()) { //your Logic } 

正则expression式类似于Facebook的validation:

 public static final String REGEX_EMAIL_VALIDATION = "^[\\w-\\+]+(\\.[\\w]+)*@[\\w-]+(\\.[\\w]+)*(\\.[a-zA-Z]{2,})$"; 

Dtounit testing(与龙目岛):

 @Data @Accessors(chain = true) @FieldDefaults(level = AccessLevel.PRIVATE) public class UserCreateDto { @NotNull @Pattern(regexp = REGEX_EMAIL_VALIDATION) @Size(min = 1, max = 254) String email; } 

以下unit testing的有效/无效电子邮件:

 public class UserCreateValidationDtoTest { private static final String[] VALID_EMAILS = new String[]{"email@yahoo.com", "email-100@yahoo.com", "Email.100@yahoo.com", "email111@email.com", "email-100@email.net", "email.100@email.com.au", "emAil@1.com", "email@gmail.com.com", "email+100@gmail.com", "emAil-100@yahoo-test.com", "email_100@yahoo-test.ABC.CoM"}; private static final String[] INVALID_EMAILS = new String[]{"あいうえお@example.com", "email@111", "email", "email@.com.my", "email123@gmail.", "email123@.com", "email123@.com.com", ".email@email.com", "email()*@gmAil.com", "eEmail()*@gmail.com", "email@%*.com", "email..2002@gmail.com", "email.@gmail.com", "email@email@gmail.com", "email@gmail.com."}; private Validator validator; @Before public void setUp() throws Exception { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); validator = factory.getValidator(); } @Test public void emailValidationShouldBeValid() throws Exception { Arrays.stream(VALID_EMAILS) .forEach(email -> { Set<ConstraintViolation<UserCreateDto>> violations = validateEmail( new UserCreateDto().setEmail(email)); System.out.println("Email: " + email + ", violations: " + violations); Assert.assertTrue(violations.isEmpty()); } ); } @Test public void emailValidationShouldBeNotValid() throws Exception { Arrays.stream(INVALID_EMAILS) .forEach(email -> { Set<ConstraintViolation<UserCreateDto>> violations = validateEmail( new UserCreateDto().setEmail(email)); System.out.println("Email: " + email + ", violations: " + violations); Assert.assertTrue(!violations.isEmpty()); } ); } private Set<ConstraintViolation<UserCreateDto>> validateEmail(UserCreateDto user) { String emailFieldName = "email"; return validator.validateProperty(user, emailFieldName); } 

}