Matching specific email address patterns with Java regular expressions

When dealing with email validation in Java, regular expressions are commonly used to match and validate the email address patterns. A regular expression (regex) is a pattern used to match strings in a text search.

Java provides the Pattern and Matcher classes from the java.util.regex package, which allow us to work with regular expressions. Let’s dive into an example of how to match specific email address patterns using Java regular expressions.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailAddressValidator {
    private static final String EMAIL_REGEX = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";

    public static void main(String[] args) {
        String[] emailAddresses = {"test@example.com", "john.doe@domain.co.uk", "invalid email", "abc@xyz"};

        Pattern pattern = Pattern.compile(EMAIL_REGEX);

        for (String email : emailAddresses) {
            Matcher matcher = pattern.matcher(email);
            System.out.println(email + " : " + (matcher.matches() ? "Valid" : "Invalid"));
        }
    }
}

Let’s break down the regular expression used to validate the email address pattern:

The Pattern class compiles the regular expression, and the Matcher class performs the actual matching against the input email addresses. The matches() method returns true if the email address matches the pattern, and false otherwise.

In the example code, we validate a list of email addresses by iterating over them using a for loop. We construct a Matcher object by calling pattern.matcher(email) and then use matcher.matches() to check if the email is valid or not.

Here are the results for each email address in the above code:

test@example.com : Valid
john.doe@domain.co.uk : Valid
invalid email : Invalid
abc@xyz : Invalid

By using regular expressions, we can easily validate email address patterns in Java. It is important to note that this regular expression might not cover all possible valid email address patterns, as the email address specification can be quite complex. However, this example serves as a starting point and can be customized based on specific requirements.

#Java #RegularExpression