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_\\-\\.~]{4,}@[a-zA-Z0-9_\\-\\.~]{3,6}\\.[a-zA-Z]{2,4}$";
final String string = "info@bvo.global\n"
+ "foo..bar+ddddd@baz.com\n\n"
+ "foo@bar.baz.com\n"
+ ".foo@bar.baz.com\n"
+ "foo.@bar.baz.com\n\n"
+ "foo-bar@baz.com\n"
+ "-foo-bar@baz.com\n"
+ "foo-bar-@baz.com\n"
+ "客服@买卖.商务\n\n"
+ "foo@bar.baz\n"
+ "foo@.bar.baz\n"
+ "foo@bar.baz.\n\n"
+ "foo@bar.baz\n"
+ "foo@bar.ba\n"
+ "foo@bar.baz.quux\n"
+ "foo@bar.b\n"
+ "foo@bar.bazquux\n\n"
+ "foo@031.33.7.255\n"
+ "42@255.255.255.255\n"
+ "42@199.1.1.1\n"
+ "42@256.255.255.255\n\n"
+ "foo@63-character-long-domain-name-which-is-allowed-by-the-standards.com\n"
+ "foo@64character-long-domain-name-which-isnt-allowed-by-the-standards.com\n\n"
+ "255-chà räçter-loñg-emà îl#%address!&with+-some_weird.!?character~combinations'in_it-just-to-fill-üp-the-address-léngth-to-get-to-the-255-character.limit.-This_is_really_starting_to_become_a_quite_long_email_address.-who_would_even_use_something_like_this?@foo.bar\n"
+ "short-address-with-illegal-character,in-it@foo.bar\n"
+ "256-character-long.-email#%address!&with+-some_weird.!?character~combinations'in_it-just-to-fill-up-the-address-length-to-get-to-the-255-character.limit.-This_is_really_starting_to_become_a_quite_long_email_address.-who_would_even_use_something_like_this?@foo.bar\n\n"
+ "foo@similarly-long-address-url.of-253-characters.long.which-is-still-allowed.within-the-standards.so-lets-just-fill-the-rest.of-this-253-character-long-address-with-lorem-ipsum.copypasta-filler-then.Lorem-ipsum-dolor-sit-amet.consectetur-adipiscing-elit.com\n"
+ "foo@similarly-long-address-urls.of-254-characters.long.which-is-not-allowed.within-the-standards.so-lets-just-fill-the-rest.of-this-254-character-long-address-with-lorem-ipsum.copy-pasta-fillers-then.Lorem-ipsum-dolor-sit-amet.consectetur-adipiscing-elit.com";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | 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