Groovy Function Tests If a String is a Valid Email Address

Posted 11/26/2016 4:21 PM by Corey Klass

Here's a simple regular expression function that will return a Boolean indicating if the specified string is an Email Address.

// is the string an email address
def isEmail(String emailAddress) {
    def emailRegEx = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2}$/

    def isEmail = !emailAddress.matches(emailRegEx)

    return isEmail
}