java regex validate email address

To validate an email address using regular expressions in Java, you can use the following regular expression pattern:

String emailPattern = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$";
Source:ww‮i.w‬giftidea.com

This regular expression pattern matches most common email addresses, with the format of local-part@domain-part, where the local part can consist of letters, digits, dots, underscores, percent signs, plus signs, and hyphens, and the domain part consists of letters, digits, dots, and hyphens.

To use this regular expression to validate an email address, you can use the following code:

String emailAddress = "[email protected]";
if (emailAddress.matches(emailPattern)) {
    System.out.println("Valid email address: " + emailAddress);
} else {
    System.out.println("Invalid email address: " + emailAddress);
}

In the example above, the "emailAddress" string is checked if it matches the "emailPattern" regular expression. If it does, it is considered a valid email address.

Note that while this regular expression pattern is a good starting point for validating email addresses, it may not catch all invalid email addresses. For example, it does not account for internationalized domain names or email addresses with quoted strings or escaped characters. It's always a good idea to double-check any email addresses entered by a user, and to use additional validation methods (such as sending a confirmation email) to ensure the address is valid.