import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^([\\w-\\.+]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
final String string = "Invalid:\n"
+ "mkyong\n"
+ "mkyong123@.com\n"
+ "mkyong()*@gmail.com\n"
+ "mkyong@%*.com\n"
+ "mkyong..2002@gmail.com\n"
+ "mkyong.@gmail.com\n"
+ "mkyong@mkyong@gmail.com\n"
+ "mkyong@gmail.com.1a\n\n"
+ "Valid:\n"
+ "mkyong@yahoo.com\n"
+ "mkyong-100@yahoo.com\n"
+ "mkyong.100@yahoo.com\n"
+ "mkyong111@mkyong.com\n"
+ "mkyong-100@mkyong.net\n"
+ "mkyong.100@mkyong.com.au\n"
+ "mkyong@1.com\n"
+ "mkyong@gmail.com.com\n"
+ "mkyong-100@yahoo-test.com\n"
+ "a.a@ac.cc.cc\n"
+ "mkyong@fdg\n"
+ ".dgfsdfg@dgf.dfg\n"
+ "mkyong+123@df.com\n"
+ "firstname+lastname@domain.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