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]|[^\\x00-\\x7F])([A-Za-z0-9@._%+-]|[^\\x00-\\x7F]){5,253}$)([A-Za-z0-9._%+-]|[^\\x00-\\x7F]){1,64}@(?:(?=([A-Za-z0-9-]|[^\\x00-\\x7F]){1,63}\\.)([A-Za-z0-9]|[^\\x00-\\x7F])+(?:-([A-Za-z0-9]|[^\\x00-\\x7F])+)*\\.){1,8}([A-Za-z]|[^\\x00-\\x7F]){2,63}$";
final String string = "Format: minimum 2 characters @ minimum 3 characters . 2 - 63 characters\n\n"
+ "the g modifier let's us match multiple times in one string. the m modifier is useful here because it lets us treat each line as a seperate entry. Though in practice you probably dont need it for a simple one line email field\n\n"
+ "^(?=[A-Z0-9][A-Z0-9@._%+-]{5,253}+$)[A-Z0-9._%+-]{1,64}+@(?:(?=[A-Z0-9-]{1,63}+\\.)[A-Z0-9]++(?:-[A-Z0-9]++)*+\\.){1,8}+[A-Z]{2,63}+$\n\n"
+ "שדגכ@שדגכ\n\n"
+ "דגכ\n\n"
+ "אלפ@גכדץ.בםצ\n\n"
+ "Jim@stuff.co.uk\n\n"
+ "Jim@google.com\n\n"
+ "Henry@h.com\n\n"
+ "Joe @m.net\n\n"
+ "Joe@m.net\n\n"
+ "Joe@mo.net\n\n"
+ "Joe@sam.net\n\n"
+ "a@sam.net\n\n"
+ "mo@sam.org\n\n"
+ "12@sam.org\n\n"
+ "Jon@sam.c\n\n"
+ "Jon@sam.co\n\n";
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