Validating email addresses is a common requirement for many Java applications.
In this article, we will discuss how to validate email addresses using regular
expressions in Java. We will cover the basics of regular expressions and then
provide examples of different email validation patterns that can be used in
Java applications.

Basics of Regular Expressions

Graphic illustrating the importance of email validation for maintaining email hygiene and reducing bounces

Regular expressions are a powerful tool for matching patterns in text. They
are a sequence of characters that define a search pattern. Regular expressions
are commonly used for validation, searching, and manipulating text.

In Java, regular expressions are represented by the java.util.regex.Pattern
class. The Pattern class provides methods for creating and manipulating
regular expressions. The regular expressions are compiled into a Pattern
object, which can then be used to match input text.

Bounceless.io banner promoting accurate email verification and improved deliverability rates

Email Validation Patterns in Java

Validating email addresses in Java can be done using regular expressions.
There are several email validation patterns that can be used, depending on the
level of validation required. Some of the most common email validation
patterns are:

  • Simple Email Validation Pattern: This pattern checks that the email address contains an @ symbol and a period. It does not check the format of the email address.
  • Strict Email Validation Pattern: This pattern checks the format of the email address, including the length of the domain name and the top-level domain.
  • Regex Email Validation Pattern: This pattern uses a regular expression to validate the email address. It checks the format of the email address, including the length of the domain name and the top-level domain.

Simple Email Validation Pattern

Illustration of a mailbox symbolizing effective email validation for accurate communication

The simple email validation pattern checks that the email address contains an
@ symbol and a period. It does not check the format of the email address. The
pattern is:

^[\w.-]+@[\w.-]+\.[A-Za-z]{2,}$

The pattern matches any string that contains one or more word characters,
followed by an @ symbol, followed by one or more word characters, followed by
a period, and ending with two or more letters.

Strict Email Validation Pattern

The strict email validation pattern checks the format of the email address,
including the length of the domain name and the top-level domain. The pattern
is:

^(?=.{1,64}@.{1,255}$)(?=.{1,64}@.{1,255}$)(?=.{1,64}@.{1,255}$)[\w!#$%&'\*+/=?^\`{|}~.-]{1,64}@([\w][\w-]{0,63}\.)+[A-Za-z]{2,6}$

The pattern matches any string that contains one or more word characters,
followed by an @ symbol, followed by one or more word characters, followed by
a period, and ending with two to six letters. The domain name must be between
one and 63 characters long, and the top-level domain must be between two and
six characters long.

Regex Email Validation Pattern

The regex email validation pattern uses a regular expression to validate the
email address. It checks the format of the email address, including the length
of the domain name and the top-level domain. The pattern is:

^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$

The pattern matches any string that contains one or more word characters,
followed by an @ symbol, followed by one or more word characters, followed by
a period, and ending with two or more letters. The domain name must be between
two and 63 characters long, and the top-level domain must be between two and
six characters long.

Conclusion

Validating email addresses is an important task in many Java applications.
Regular expressions provide a powerful tool for matching patterns in text. In
this article, we have discussed how to validate email addresses using regular
expressions in Java. We have covered the basics of regular expressions and
provided examples of different email validation patterns that can be used in
Java applications.