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]
import java.util.Scanner;
public class CheckingTheEmailPassword {
public static void main(String[] args) {
String email = null;
String password = null;
Boolean password_valid = false;
Boolean email_valid = false;
Scanner input = new Scanner(System.in);
do {
System.out.println("Enter your email: ");
email = input.nextLine();
System.out.println("Enter your passsword: ");
password = input.nextLine();
// checks for words,numbers before @symbol and between "@" and ".".
// Checks only 2 or 3 alphabets after "."
if (email.matches("[\\w]+@[\\w]+\\.[a-zA-Z]{2,3}"))
email_valid = true;
else
email_valid = false;
// checks for NOT words,numbers,underscore and whitespace.
// checks if special characters present
if ((password.matches(".*[^\\w\\s].*")) &&
// checks alphabets present
(password.matches(".*[a-zA-Z].*")) &&
// checks numbers present
(password.matches(".*[0-9].*")) &&
// checks length
(password.length() >= 8))
password_valid = true;
else
password_valid = false;
if (password_valid && email_valid)
System.out.println(" Welcome User!!");
else {
if (!email_valid)
System.out.println(" Re-enter your email: ");
if (!password_valid)
System.out.println(" Re-enter your password: ");
}
} while (!email_valid || !password_valid);
input.close();
}
}
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.
Modification of Armer B. answer which didn't validate emails ending with '.co.uk'
public static boolean emailValidate(String email) {
Matcher matcher = Pattern.compile("^([\\w-\\.]+){1,64}@([\\w&&[^_]]+){2,255}(.[a-z]{2,3})+$|^$", Pattern.CASE_INSENSITIVE).matcher(email);
return matcher.find();
}
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
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();
}
}
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.