import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^[a-z0-9]+[\\._]?[a-z0-9]+[@]\\w+[.]\\w{2,3}$";
final String string = "###################### Valid Emails ########################################################\n"
+ "######################## German, Austrian, international ###################################\n\n"
+ "janedoe@web.de\n"
+ "jane.doe@web.de\n"
+ "jdoe@web.de\n"
+ "jane-doe@web.de\n"
+ "jane@web.de\n"
+ "doe@web.de\n"
+ "janed@web.de\n"
+ "doej@web.de\n"
+ "jane_doe@web.de\n"
+ "j.doe@web.de\n"
+ "jane.d@web.de\n\n"
+ "janedoe@t-online.de\n"
+ "jane.doe@t-online.de\n"
+ "jdoe@t-online.de\n"
+ "jane-doe@t-online.de\n"
+ "jane@t-online.de\n"
+ "janed@t-online.de\n"
+ "doej@t-online.de\n"
+ "jane_doe@t-online.de\n"
+ "j.doe@t-online.de\n"
+ "jane.d@t-online.de\n\n"
+ "janedoe@hotmail.de\n"
+ "jane.doe@hotmail.de\n"
+ "jdoe@hotmail.de\n"
+ "jane-doe@hotmail.de\n\n"
+ "janedoe@gmx.de\n"
+ "jane.doe@gmx.de\n"
+ "jdoe@gmx.de\n"
+ "jane-doe@gmx.de\n\n\n"
+ "jane.doe@post.at\n"
+ "jane-doe@post.at\n"
+ "janedoe@post.at\n"
+ "jane@post.at\n"
+ "john.doe@austrian.com\n"
+ "horneaudrey@fulbright.at\n\n\n"
+ "#####################################################################################\n"
+ "########################## Not valid email ##########################################\n"
+ ".test@gmail.com\n"
+ "-test@gmail.com\n"
+ "t.-.est@gmail.com\n"
+ ".test.@gmail.com\n"
+ "test.@gmail.com\n"
+ "test.@.gmail.com\n"
+ "test@.gmail.com\n"
+ "test@.gmail.com.\n"
+ "test@gmail.com.\n\n"
+ "\"email\"@example.com\n\n"
+ "#@%^%#$@#$@#.com\n"
+ "@example.com\n"
+ "Joe Smith <email@example.com>\n"
+ "email.example.com\n"
+ "email@example@example.com\n"
+ ".email@example.com\n"
+ "email.@example.com\n"
+ "email..email@example.com\n"
+ "あいうえお@example.com\n"
+ "email@example.com (Joe Smith)\n"
+ "email@example\n"
+ "email@-example.com\n"
+ "email@example.web\n"
+ "email@111.222.333.44444\n"
+ "email@example..com\n"
+ "Abc..123@example.com";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html