Java regex email
Clash Royale CLAN TAG#URR8PPP
Java regex email
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]2,4b
In Java, I did this:
Pattern p = Pattern.compile("\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]2,4\b");
Matcher m = p.matcher("foobar@gmail.com");
if (m.find())
System.out.println("Correct!");
However, the regex fails regardless of whether the email is wel-formed or not. A "find and replace" inside Eclipse works fine with the same regex.
Any idea?
Thanks,
Why is ti not recommended to use regex for email validation in Java?
– Filipe Miranda
Jun 9 '15 at 16:18
emailregex.com use the regex given from this site. It claims to have 99.99% correct email regex
– Menuka Ishan
Dec 15 '16 at 9:49
16 Answers
16
FWIW, here is the Java code we use to validate email addresses. The Regexp's are very similar:
public static final Pattern VALID_EMAIL_ADDRESS_REGEX =
Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]2,6$", Pattern.CASE_INSENSITIVE);
public static boolean validate(String emailStr)
Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(emailStr);
return matcher.find();
Works fairly reliably.
Just two counterexamples: webmaster@müller.de (valid and rejected by your example), matteo@78.47.122.114 (my email, valid and rejected by your example.
– Matteo
Nov 21 '11 at 5:58
This regex is incomplete. See my answer for an RFC 822 compliant validation.
– ejboy
Mar 11 '13 at 15:28
For a simple solution that matches 99.9% of email addresses this is a good solution.
– maloney
Aug 8 '13 at 11:21
Check this regex it matches both of your emails @Matteo
– T04435
Oct 18 '15 at 2:52
@T04435 The regex in you link does not escape the DOT. What makes the regex functionally wrong and this also has a serious performance impact
– TomWolk
Jun 2 '16 at 8:57
Here is RFC822 compliant regex adapted for Java:
Pattern ptr = Pattern.compile("(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*:(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)(?:,\s*(?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*))*)?;\s*)");
String emails = ""Fred Bloggs"@example.com", "user@.invalid.com", "Chuck Norris <gmail@chucknorris.com>", "webmaster@müller.de", "matteo@78.47.122.114" ;
for (String email : emails)
System.out.println(email + " is " + (ptr.matcher(email).matches() ? "valid" : "invalid"));
Output:
"Fred Bloggs"@example.com is valid
user@.invalid.com is invalid
Chuck Norris <gmail@chucknorris.com> is valid
webmaster@müller.de is valid
matteo@78.47.122.114 is valid
The regex is taken from this post: Mail::RFC822::Address: regexp-based address validation. The results should coincide with online version.
What is the reason for downvoting? This regexp really works!
– ejboy
Nov 7 '12 at 14:42
I think he downgraded it because one has to manually escape special characters like " before compile. Unless there will be syntax error.
– Isuru Madusanka
Mar 8 '13 at 19:37
Ok, I've made a working Java example.
– ejboy
Mar 11 '13 at 13:05
This pattern validates against RFC822 spec, but does does not answer whether the email address points to an existing mail server. Additionally, top level domains are not limited to just 3 chars. There are longer TLDs and the list is growing. IMO the only way to check validity for cases when one wants to check for existence of the mail server is to resolve the host name through DNS, and/or try to lookup the mx records.
– ejboy
Feb 6 '14 at 15:59
this validates
email@email
as an valid email– lxknvlk
Aug 2 '16 at 13:59
email@email
Don't. You will never end up with a valid expression.
For example these are all valid email addresses:
"Abc@def"@example.com
"Fred Bloggs"@example.com
"Joe\Blow"@example.com
"Abc@def"@example.com
customer/department=shipping@examp le.com
$A12345@example.com
!def!xyz%abc@example.com
_somename@example.com
matteo(this is a comment).corti@example.com
root@[127.0.0.1]
Just to mention a few problems:
Before even beginning check the corresponding RFCs
E-mails with spaces? That's seems pretty invalid even if somewhere it was decided that e-mails can have spaces.
– The Berga
Jul 25 '16 at 11:25
emailregex.com this site claims to have 99% percent correct regex for emails
– Menuka Ishan
Dec 15 '16 at 9:45
@MenukaIshan As they claim themselves regex will never be completely OK. You can test several examples above. Now the question is why stick to regeres where there are implementations that work?
– Matteo
Dec 15 '16 at 9:49
@Matteo Can you give me such example that don't require regex? I know about Hibernate Validator @ Email annotation. Is that type of things are you talking about? Anyway I would love to know your examples so that I will able to use them in my future developments. Thank you.
– Menuka Ishan
Dec 15 '16 at 10:51
Which language are using?
– Matteo
Dec 15 '16 at 11:51
That's because you are forgetting case insensitivity :
Pattern regex = Pattern.compile("\b[\w.%-]+@[-.\w]+\.[A-Za-z]2,4\b");
This matches your example, although it ignores many valid e-mails.
+1 for the "ignores many valid e-mails"
– Matteo
Nov 21 '11 at 5:59
One another simple alternative to validate 99% of emails
public static final String EMAIL_VERIFICATION = "^([\w-\.]+)1,64@([\w&&[^_]]+)2,255.[a-z]2,$";
Is maching set to CASE_INSENSITIVE?
You can use this method for validating email address in java.
public class EmailValidator
private Pattern pattern;
private Matcher matcher;
private static final String EMAIL_PATTERN =
"^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]2,)$";
public EmailValidator()
pattern = Pattern.compile(EMAIL_PATTERN);
/**
* Validate hex with regular expression
*
* @param hex
* hex for validation
* @return true valid hex, false invalid hex
*/
public boolean validate(final String hex)
matcher = pattern.matcher(hex);
return matcher.matches();
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
This is a valid regex for validating e-mails. It's totally compliant with RFC822 and accepts IP address and server names (for intranet purposes).
public static boolean isEmailValid(String email)
final Pattern EMAIL_REGEX = Pattern.compile("[a-z0-9!#$%&'*+/=?^_`~-]+(?:.[a-z0-9!#$%&'*+/=?^_`~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", Pattern.CASE_INSENSITIVE);
return EMAIL_REGEX.matcher(email).matches();
Here are some output examples, when you call isEmailValid(emailVariable)
:
isEmailValid(emailVariable)
john@somewhere.com // valid
john.foo@somewhere.com // valid
john.foo+label@somewhere.com // valid (with +label - Gmail accepts it!)
john@192.168.1.10 // valid (with IP addresses)
john+label@192.168.1.10 // valid (with +label and IP address)
john.foo@someserver // valid (with no first domain level)
JOHN.FOO@somewhere.com // valid (case insensitive)
@someserver // invalid
@someserver.com // invalid
john@. // invalid
.@somewhere.com // invalid
Regex : ^[\w!#$%&’*+/=?
~^-]+(?:.[w!#$%&’*+/=?~^-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]2,6$
^[\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.
"^[\pL\pN\._%+-]+@[\pL\pN\.\-]+\.[\pL]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.pL
validates UTF-Letters and pN
validates UTF-Numbers. You can check this doc for more information.
xxx@xxx.TDL
pL
pN
Regex for Facebook-like validation:
public static final String REGEX_EMAIL_VALIDATION = "^[\w-\+]+(\.[\w]+)*@[\w-]+(\.[\w]+)*(\.[a-zA-Z]2,)$";
Dto for Unit tests(with Lombok):
@Data
@Accessors(chain = true)
@FieldDefaults(level = AccessLevel.PRIVATE)
public class UserCreateDto
@NotNull
@Pattern(regexp = REGEX_EMAIL_VALIDATION)
@Size(min = 1, max = 254)
String email;
Valid/invalid emails below with Unit tests:
public class UserCreateValidationDtoTest
private static final String VALID_EMAILS = new String"email@yahoo.com", "email-100@yahoo.com",
"Email.100@yahoo.com", "email111@email.com", "email-100@email.net",
"email.100@email.com.au", "emAil@1.com", "email@gmail.com.com",
"email+100@gmail.com", "emAil-100@yahoo-test.com", "email_100@yahoo-test.ABC.CoM";
private static final String INVALID_EMAILS = new String"あいうえお@example.com", "email@111",
"email", "email@.com.my", "email123@gmail.", "email123@.com", "email123@.com.com",
".email@email.com", "email()*@gmAil.com", "eEmail()*@gmail.com", "email@%*.com", "email..2002@gmail.com",
"email.@gmail.com", "email@email@gmail.com", "email@gmail.com.";
private Validator validator;
@Before
public void setUp() throws Exception
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
@Test
public void emailValidationShouldBeValid() throws Exception
Arrays.stream(VALID_EMAILS)
.forEach(email ->
Set<ConstraintViolation<UserCreateDto>> violations = validateEmail(
new UserCreateDto().setEmail(email));
System.out.println("Email: " + email + ", violations: " + violations);
Assert.assertTrue(violations.isEmpty());
);
@Test
public void emailValidationShouldBeNotValid() throws Exception
Arrays.stream(INVALID_EMAILS)
.forEach(email ->
Set<ConstraintViolation<UserCreateDto>> violations = validateEmail(
new UserCreateDto().setEmail(email));
System.out.println("Email: " + email + ", violations: " + violations);
Assert.assertTrue(!violations.isEmpty());
);
private Set<ConstraintViolation<UserCreateDto>> validateEmail(UserCreateDto user)
String emailFieldName = "email";
return validator.validateProperty(user, emailFieldName);
Try the below code for email is format of
jsmith@example.com
1st part -jsmith
2nd part -@example.com
-jsmith
-@example.com
1. In the 1 part it will allow 0-9,A-Z,dot sign(.),underscore sign(_)
2. In the 2 part it will allow A-Z, must be @ and .
^[a-zA-Z0-9_.]+@[a-zA-Z.]+?.[a-zA-Z]2,3$
I have tested this below regular expression
for single and multiple consecutive dots in domain
name -
regular expression
domain
([A-Za-z0-9-_.]+@[A-Za-z0-9-_]+(?:.[A-Za-z0-9]+)+)
and here are the examples which were completely fulfilled by above regex
.
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...
email id's validation
regex
If you still know some consequentially used email id's
had left here, please let me know in comment section!
email id's
String emailRegex = "[a-zA-Z0-9_.]+@[a-zA-Z0-9]+.[a-zA-Z]2,3[.] 0,1[a-zA-Z]+";
Pattern.matches(emailRegex,"You_Input_Mail_Id");
This is the regex to match valid email addresses.
Try adding some formatting and context to your code to help future readers better understand its meaning.
– Grant Miller
Jul 5 at 15:50
try this
String regex = "^[\pL\pN\._%+-]+@[\pL\pN\.\-]+\.[\pL]2,$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(Email);
if (matcher.matches())
//your Logic
Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
– WhatsThePoint
Oct 4 '17 at 12:32
I simply don't think that any codder with a bit of experience will require any explanation to what I've written.
– Sohil Ahmed
Oct 5 '17 at 3:56
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
One problem with your regexp is case-sensitivity. You should be using the Patterm.compile("...", Pattern.CASE_INSENSITIVE) constructor for your pattern.
– Jason Buberel
Nov 20 '11 at 21:05