I’m trying to validate a e-mail string in Java but I’m getting trouble to validate when this string has two or more special chars toghether, which is not a valid e-mail.
My if that does this validation is the following:
if (email.matches("(._\'!#$%&*+-\\/=?{|}~`^_-)\\1+")) {
return false;
}
Example for the output:
this_e-mail’s_valid@domain.com – return true (CORRECT)
this_e-mail_isn”t_valid@domain.com – return true (WRONG)
Obviously there’s something wrong with my regular expression.
But I looked all over the internet to find some answer but didn’t succeeded.
I’ve just read that using the “\1+” before expression, it was supposed to do this validation, but apparently it doesn’t.
Thanks in advance for the answers!
I am not at all sure about your requirement. Email validation should be more than just this.
However, if you want to detect whether a string has 2 special characters consecutively or not, you can use
Note that I logically negate the result. I’m matching the string if it contains consecutive characters.
I have tested and rectify the error in earlier revision.