First of all, I know that using regex for email is not recommended but I gotta test this out.
I have this regex:
\\b[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]
General Email format (RE) which include also domain like co.in, co.uk, com, outlook.com etc.
And rule says that :
(dot, period, full stop) provided that it is not the first or last character, and provided also that it does not appear two or more times consecutively.
[a-zA-Z0-9]+[._a-zA-Z0-9!#$%&'*+-/=?^_`{|}~]*[a-zA-Z]*@[a-zA-Z0-9]{2,8}.[a-zA-Z.]{2,6}
One another simple alternative to validate 99% of emails
public static final String EMAIL_VERIFICATION = "^([\\w-\\.]+){1,64}@([\\w&&[^_]]+){2,255}.[a-z]{2,}$";
I have tested this below regular expression
for single and multiple consecutive dots in domain
name -
([A-Za-z0-9-_.]+@[A-Za-z0-9-_]+(?:\.[A-Za-z0-9]+)+)
and here are the examples which were completely fulfilled by above regex
.
End_user@live.com
End.u@exm-tech.net
enduser9876@gmail.in
end_user@mywebsite.ac.in.gui
Another984.User2@mail.edu.sg
Another987_User5@mail.show.au
Slow_User@example_domain.au.in
iamthemostsimpleremailhere@example.com
I have tried to cover maximum commonly used email id's validation
by this above illustrated regex
and yet working...
If you still know some consequentially used email id's
had left here, please let me know in comment section!
Regex : ^[\\w!#$%&’*+/=?
{|}~^-]+(?:\.[\w!#$%&’*+/=?{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$
public static boolean isValidEmailId(String email) {
String emailPattern = "^[\\w!#$%&’*+/=?`{|}~^-]+(?:\\.[\\w!#$%&’*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";
Pattern p = Pattern.compile(emailPattern);
Matcher m = p.matcher(email);
return m.matches();
}
If you want to allow non-latain characters, this one works quite well for me.
"^[\\p{L}\\p{N}\\._%+-]+@[\\p{L}\\p{N}\\.\\-]+\\.[\\p{L}]{2,}$"
It does not allow IP's after the @ but most valid email in the from of xxx@xxx.TDL
could be validated with it.
\p{L}
validates UTF-Letters and \p{N}
validates UTF-Numbers. You can check this doc for more information.
You can checking if emails is valid or no by using this libreries, and of course you can add array for this folowing project.
import org.apache.commons.validator.routines.EmailValidator;
public class Email{
public static void main(String[] args){
EmailValidator email = EmailVlidator.getInstance();
boolean val = email.isValid("george.parcks@gmail.com");
System.out.println("Mail is: "+val);
val = email.isValid("hans.riguer.hotmsil.com");
System.out.print("Mail is: "+val");
}
}
output :
Mail is: true
Mail is : false