Java正则表达式基本电子邮件验证

huangapple go评论64阅读模式
英文:

Java regex basic email validation

问题

I want to do basic email validation in java using regex and I came up with following regex

private static final String EMAIL_REGEX = "^\\s*(.+)@(.+)\\.(.+)\\s*$";

If we take test@gmail.com under this regex.

  1. Ignore starting and trailing white spaces.
  2. 1 or more char for username (test)
  3. @
  4. one or more char for domain (gmail)
  5. .
  6. one or more char for domain extension(com).

Is it correct as per my requirement?

I have checked Apache EmailValidator but it does some validation of domain also, eg. when i tried it with test@gmail.adpcc, then it did not work. And I want it to work as per my use case.

英文:

I want to do basic email validation in java using regex and I came up with following regex

private static final String EMAIL_REGEX = "^\\s*(.+)@(.+)\\.(.+)\\s*$";

If we take test@gmail.com under this regex.

1. Ignore starting and trailing white spaces.
2. 1 or more char for username  (test)
3. @
4. one or more char for domain (gmail)
5. .
6. one or more char for domain extension(com).

Is it correct as per my requirement?

I have checked Apache EmailValidator but it does some validation of domain also, eg. when i tried it with test@gmail.adpcc, then it did not work. And I want it to work as per my use case.

答案1

得分: -1

你提供的正则表达式是一个基本的电子邮件验证模式,看起来符合你的要求。但是,它并不是非常严格,可能允许一些无效的电子邮件地址通过。以下是你提供的正则表达式:

private static final String EMAIL_REGEX = "^\\s*(.+)@(.+)\\.(.+)\\s*$";

这个正则表达式将匹配符合你描述的格式的电子邮件地址,忽略前导和尾随空格,并要求用户名、域和域扩展至少包含一个字符。然而,它不会对用户名、域或域扩展中可以出现的字符类型施加任何限制。

如果你想让正则表达式稍微更严格一些,可以使用以下模式:

private static final String EMAIL_REGEX = "^\\s*([\\w!#$%&'*+\\-/=?^_`{|}~]+(?:\\.[\\w!#$%&'*+\\-/=?^_`{|}~]+)*)@([\\w](?:[\\w-]*[\\w])?\\.)+[a-zA-Z]{2,}\\s*$";

这个模式允许在用户名和域中包含更广泛范围的有效字符,同时检查至少包含两个字母字符的有效域扩展。然而,它仍然可能无法涵盖所有有效的电子邮件地址或拒绝所有无效的地址。

Apache Commons EmailValidator 在验证方面更为全面,但正如你提到的,它可能不适合你的用例。如果上述的正则表达式模式符合你的特定要求,那么你可以将其用于基本的电子邮件验证。

英文:

The regex you provided is a basic email validation pattern, and it seems to meet your requirements as described. However, it is not very strict and may allow some invalid email addresses to pass through. Here's the regex you provided:

private static final String EMAIL_REGEX = "^\\s*(.+)@(.+)\\.(.+)\\s*$";

This regex will match email addresses with the format you described, ignoring leading and trailing whitespace, and requiring at least one character for the username, domain, and domain extension. However, it does not impose any restrictions on the types of characters that can appear in the username, domain, or domain extension.

If you want to improve the regex to be slightly more strict, you can use the following pattern:

private static final String EMAIL_REGEX = "^\\s*([\\w!#$%&'*+\\-/=?^_`{|}~]+(?:\\.[\\w!#$%&'*+\\-/=?^_`{|}~]+)*)@([\\w](?:[\\w-]*[\\w])?\\.)+[a-zA-Z]{2,}\\s*$";

This pattern allows for a wider range of valid characters in the username and domain, while also checking for valid domain extensions consisting of at least two alphabetic characters. However, it still may not cover all valid email addresses or reject all invalid ones.

The Apache Commons EmailValidator is more comprehensive in its validation, but as you mentioned, it may not suit your use case. If the regex pattern above works for your specific requirements, then you can use it for your basic email validation.

huangapple
  • 本文由 发表于 2023年5月11日 01:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定