-1

I have created a regex for email validation which will check the email is in proper format or not.

Regular expression for the email is

[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}[.]{0,1}[a-zA-Z]{0,2}

but when i am putting an email address like shantanu0076@abc.abcedf.com

this regex expression is not working

How can i solve this issue

3

Your regular expression doesn't do what you think it does. ., by itself, matches any character, not just a .. You need to escape it to specify a literal period. Also, note that emails are a lot more complicated than you think they are; see this answer for a current regular expression that should match all possible valid emails. Translated to Apex, it looks like:

static Pattern emailPattern = Pattern.compile('(?:[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*|"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])');
public static Boolean isValidEmail(String email) {
    return emailPattern.matcher(email).matches();
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.