Search code examples
hibernate-validator

What precisely is the regex pattern that Hibernate validator employes for @Email?


Versions:

hibernate-validator: 6.2.0.Final (from mvnrepository.com)

If needs be, you can peruse relevant docs at jboss.org.

I've discovered that out-of-the-box that Hibernate validator (through the @Email annotation) supports validation of E-mail addresses (that is, for strings that should house valid E-mail addresses).

QUESTION: does anyone know the default regexp that the Hibernate Validator 6.2 employs for @Email validation?


Solution

  • From the hibernate-validator source code:

    • Checks that a given character sequence (e.g. string) is a well-formed email address.
    • The specification of a valid email can be found in RFC 2822 and one can come up with a regular expression matching all valid email addresses as per specification. However, as this article discusses it is not necessarily practical to implement a 100% compliant email validator. This implementation is a trade-off trying to match most email while ignoring for example emails with double quotes or comments.
    private static final int MAX_LOCAL_PART_LENGTH = 64;
    
    private static final String LOCAL_PART_ATOM = "[a-z0-9!#$%&'*+/=?^_`{|}~\u0080-\uFFFF-]";
    private static final String LOCAL_PART_INSIDE_QUOTES_ATOM = "(?:[a-z0-9!#$%&'*.(),<>\\[\\]:;  @+/=?^_`{|}~\u0080-\uFFFF-]|\\\\\\\\|\\\\\\\")";
    /**
     * Regular expression for the local part of an email address (everything before '@')
     */
    private static final Pattern LOCAL_PART_PATTERN = Pattern.compile(
            "(?:" + LOCAL_PART_ATOM + "+|\"" + LOCAL_PART_INSIDE_QUOTES_ATOM + "+\")" +
                    "(?:\\." + "(?:" + LOCAL_PART_ATOM + "+|\"" + LOCAL_PART_INSIDE_QUOTES_ATOM + "+\")" + ")*", CASE_INSENSITIVE
    );
    

    See also the regex for the domain part.