import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?![^a-zA-Z0-9])(?:(?![.]{2,})[a-zA-Z0-9_\\-.])+(?<![.])@(?:(?![^a-zA-Z0-9])(?:[a-zA-Z0-9_\\-])+(?<![^a-zA-Z0-9])\\.)+[a-zA-Z]{2,}$";
final String string = "email@example.com\n"
+ "email@111.222.333.fr\n"
+ "firstname.lastname@example.com\n"
+ "email@subdomain.example.com\n"
+ "123@gmail.com\n"
+ "123@51gmail62548.com\n"
+ "1234567890@example.com\n"
+ "a.defosseux@tbs-education.fr\n"
+ "email@example-one.com\n"
+ "email@example.name\n"
+ "email@example.museum\n"
+ "email@example.co.jp\n"
+ "firstname-lastname@example.com\n"
+ "test@test.com\n"
+ "ezf@aezf.fr.en\n"
+ "azer123@123.fr\n"
+ "a@a.bb\n"
+ "a@a.ccc\n"
+ "a@bb.bb\n"
+ "a.a@bb.bb\n"
+ "a.a.a@bb.bb\n"
+ "a.a.a@a.bb.bb\n"
+ "a.a.a.a@a.a.bb\n"
+ "a.a.a.a@a.bb.bb\n"
+ "a.a.a.a@a.bb.ccc\n"
+ "a.bb.a.a@a.bb.ccc\n"
+ "a--a.a@a.bb.ccc\n"
+ "a.defosseux@tbs-education.x.fr\n"
+ "a.defosseux@tbs-education.x.y.fr\n"
+ "a.defosseux@tbs-education.x-e.y.fr\n"
+ "email@example-one.com\n"
+ "ema.il@example.com\n"
+ "e.ma.il@example.com\n"
+ "ema-il@example.com\n"
+ "e-ma-il@example.com\n"
+ "ema--il@example.com\n"
+ "em_ail@example.com\n"
+ "em__ail@example.com\n"
+ "6email@example.com\n"
+ "email6@example.com\n"
+ "6email6@example.com\n"
+ "test-test@x.fr\n"
+ "tes-t-test@x.fr\n"
+ "yasmina.achak@e.rascol.net\n"
+ "yasmina.achak@e.rascol.net.aezr.azerz.azer.azreezr.azeraez.azer\n"
+ "yasmina.achak@e.rascol.barce\n"
+ "yasmina.achak@e.rascol.bar\n"
+ "yasmina.achak@e.ba\n"
+ "sarah_benabdell@yahoo.fr\n"
+ "sarah__benabdellah@yahoo.fr\n"
+ "sarah---benabdellah@yahoo.fr\n"
+ "sarah___---benabdellah@yahoo.fr\n"
+ "marieagnes.paul@off---white.com\n"
+ "a.a@bb--bb.ccc\n"
+ "a.a@bb--bb.a.ccc\n"
+ "a_a@a.bb\n"
+ "a_a@a.bb\n"
+ "email-@example.com\n"
+ "email_@example.com\n"
+ "a_@a.bb\n"
+ "a._@a.bb\n\n"
+ "a@ccc\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..email@example.com\n"
+ "あいうえお@example.com\n"
+ "email@example.com (Joe Smith)\n"
+ "email@example\n"
+ "email@-example.com\n"
+ "email@111.222.333.44444\n"
+ "email@example..com\n"
+ "a..reds@free.fr\n"
+ "Abc..123@example.com\n"
+ "”(),:;<>[\\]@example.com\n"
+ "just”not”right@example.com\n"
+ "this\\ is\"really\"not\\allowed@example.com\n"
+ "firstname+lastname@example.com\n"
+ "email@123.123.123.123\n"
+ "email@[123.123.123.123]\n"
+ "\"email\"@example.com\n"
+ "_______@example.com\n"
+ "much.\"more\\ unusual\"@example.com\n"
+ "very.unusual.”@”.unusual.com@example.com\n"
+ "very.”(),:;<>[]”.VERY.”very@\\\\ \"very”.unusual@strange.example.com\n"
+ "azer aezr@test.com\n"
+ "zaetazfe.faezf\n"
+ "af@ezfezf\n"
+ "ez#!~f@aezf.fr\n"
+ "azefé@test.com\n"
+ "aze@zaé.com\n"
+ "a@a.a\n"
+ "a.a@a.a\n"
+ "a.a@a.a.a\n"
+ "a.a.a.a@a.a.a\n"
+ "a.a.a.a@a.a.a.a\n"
+ "a..a.a@a.bb.ccc\n"
+ "a.é@b.fr\n"
+ "azer.@fr\n"
+ ".azer@fr\n"
+ "a.defosseux@tbs-education-.fr\n"
+ "a.defosseux@-tbs-education.fr\n"
+ "a.defosseux@-tbs-education-.fr\n"
+ "a.defosseux@1234tbs-education.-x-.fr\n"
+ ".email@example.com\n"
+ ".email.@example.com\n"
+ "email.@example.com\n"
+ "ema..il@example.com\n"
+ "-email@example.com\n"
+ "-email-@example.com\n"
+ "_email@example.com\n"
+ "_email_@example.com\n"
+ "_em_ail_@example.com\n"
+ "_em__ail_@example.com\n"
+ "yaémina.achak@e.rascol.net\n"
+ ".-y__as-min-.@eerez.fr\n"
+ "yasmina.achak@e.b\n"
+ "yasmina.achak@e....bar\n"
+ "_a_@a.bb\n"
+ "_a@a.bb\n"
+ "_.a@a.bb\n"
+ "_.a._@a.bb\n"
+ "a@_a.bb\n"
+ "a@a_.bb\n"
+ "a@_a_.bb\n"
+ "a@a._bb_.ccc\n"
+ "a@a.__a__a__.ccc\n"
+ "a@a.__.bb.ccc";
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