import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^ (?&address) $\n"
+ "(?(DEFINE)\n"
+ " (?<ALPHANUMERICE> \n"
+ " [A-Za-z0-9!\\#$%&'*+\\-\\/=?^_`\\{|\\}~]\n"
+ " )\n"
+ " (?<CFWS> \n"
+ " (?: \n"
+ " (?: (?&FWS)? (?&comment) )++ \n"
+ " (?&FWS)?\n"
+ " ) \n"
+ " | (?&FWS) \n"
+ " )\n"
+ " (?<FWS> (?&WSPCRLF)++ )\n"
+ " (?<UTF8_extra_char> \n"
+ " (?=[\\x{C2}-\\x{DF}\\x{E0}-\\x{EF}\\x{F0}-\\x{F4}]) \n"
+ " (?: (?&UTF8_2) | (?&UTF8_3) | (?&UTF8_4) ) \n"
+ " )\n"
+ " (?<UTF8_tail> [\\x{80}-\\x{BF}] )\n"
+ " (?<UTF8_2> [\\x{C2}-\\x{DF}] (?&UTF8_tail) )\n"
+ " (?<UTF8_3> \\x{E0} [\\x{A0}-\\x{BF}] (?&UTF8_tail)\n"
+ " | [\\x{E1}-\\x{EC}] (?&UTF8_tail){2} \n"
+ " | \\x{ED} [\\x{80}-\\x{9F}] (?&UTF8_tail) \n"
+ " | [\\x{EE}\\x{EF}] (?&UTF8_tail){2}\n"
+ " )\n"
+ " (?<UTF8_4> \\x{F0} [\\x{90}-\\x{BF}] (?&UTF8_tail){2}\n"
+ " | [\\x{F1}-\\x{F3}] (?&UTF8_tail){3}\n"
+ " | \\x{F4} [\\x{80}-\\x{8F}] (?&UTF8_tail){2}\n"
+ " )\n"
+ " (?<VCHARWSP> [\\x{21}-\\x{7E}\\ \\t] )\n"
+ " (?<WSPCRLF> [\\ \\t\\x{0D}\\x{0A}] )\n"
+ " (?<address> (?&mailbox_list) | (?&group) )\n"
+ " (?<addr_spec> (?&local_part) @ (?&domain) )\n"
+ " (?<utf8_addr_spec> \n"
+ " (?&utf8_local_part) @ (?&utf8_domain) \n"
+ " )\n"
+ " (?<alt_address> (?&FWS) < (?&addr_spec) > )\n"
+ " (?<angle_addr> (?&CFWS)? < \n"
+ " (?: (?&addr_spec) \n"
+ " | (?&utf8_addr_spec) (?&alt_address)? \n"
+ " | (?&obs_route)? (?&addr_spec) \n"
+ " ) \n"
+ " > (?&CFWS)? \n"
+ " )\n"
+ " (?<atext> (?&ALPHANUMERICE) )\n"
+ " (?<utf8_atext> \n"
+ " (?&ALPHANUMERICE) \n"
+ " | (?&UTF8_extra_char) \n"
+ " )\n"
+ " (?<atom> (?&CFWS)? (?&atext)++ (?&CFWS)? )\n"
+ " (?<utf8_atom> \n"
+ " (?&CFWS)? (?&utf8_atext)++ (?&CFWS)? \n"
+ " )\n"
+ " (?<utf8_ccontent> \n"
+ " (?&ctext) \n"
+ " | (?&utf8_quoted_pair) \n"
+ " | (?&comment) \n"
+ " )\n"
+ " (?<comment> \n"
+ " \\( \n"
+ " (?: \n"
+ " (?&FWS)? (?&utf8_ccontent) \n"
+ " )*+ \n"
+ " (?&FWS)? \n"
+ " \\) \n"
+ " )\n"
+ " (?<ctext> \n"
+ " [!-'*-\\[\\]-~] \n"
+ " | (?&obs_ctext) \n"
+ " | (?&UTF8_extra_char) \n"
+ " )\n"
+ " (?<display_name> (?&phrase) )\n"
+ " (?<domain> \n"
+ " (?&dot_atom) \n"
+ " | (?&domain_literal) \n"
+ " | (?&obs_domain) \n"
+ " )\n"
+ " (?<utf8_domain> \n"
+ " (?&utf8_dot_atom) \n"
+ " | (?&domain_literal) \n"
+ " | (?&obs_domain) \n"
+ " )\n"
+ " (?<domain_literal> \n"
+ " (?&CFWS)? \n"
+ " \\[ \n"
+ " (?: (?&FWS)? (?&dtext) )*+ \n"
+ " (?&FWS)? \n"
+ " \\] \n"
+ " (?&CFWS)? \n"
+ " )\n"
+ " (?<dot_atom> \n"
+ " (?&CFWS)? (?&dot_atom_text) (?&CFWS)? \n"
+ " )\n"
+ " (?<utf8_dot_atom> \n"
+ " (?&CFWS)? (?&utf8_dot_atom_text) (?&CFWS)? \n"
+ " )\n"
+ " (?<dot_atom_text> \n"
+ " (?&atext)++ \n"
+ " (?: \\. (?&atext)++ )*+ \n"
+ " )\n"
+ " (?<utf8_dot_atom_text> \n"
+ " (?&utf8_atext)++ \n"
+ " (?: \\. (?&utf8_atext)++ )*+ \n"
+ " )\n"
+ " (?<dtext> [!-Z^-~] | (?&obs_dtext) )\n"
+ " (?<group> \n"
+ " (?&display_name) : (?&group_list)? ; (?&CFWS)? \n"
+ " )\n"
+ " (?<group_list> \n"
+ " (?&mailbox_list) \n"
+ " | (?&CFWS) \n"
+ " | (?&obs_group_list) \n"
+ " )\n"
+ " (?<local_part> \n"
+ " (?&dot_atom) \n"
+ " | (?"ed_string) \n"
+ " | (?&obs_local_part) \n"
+ " )\n"
+ " (?<utf8_local_part> \n"
+ " (?&utf8_dot_atom) \n"
+ " | (?&utf8_quoted_string) \n"
+ " | (?&obs_local_part) \n"
+ " )\n"
+ " (?<mailbox> \n"
+ " (?&name_addr) \n"
+ " | (?&addr_spec) \n"
+ " | (?&utf8_addr_spec) \n"
+ " )\n"
+ " (?<mailbox_list> \n"
+ " (?: \n"
+ " (?&mailbox) (?: , (?&mailbox) )*+ \n"
+ " ) \n"
+ " | (?&obs_mbox_list) \n"
+ " ) \n"
+ " (?<name_addr> (?&display_name)? (?&angle_addr) )\n"
+ " (?<obs_NO_WS_CTL> \n"
+ " [\\x{01}-\\x{08}\\x{0B}\\x{0C}\\x{0E}-\\x{1F}\\x{7F}] \n"
+ " )\n"
+ " (?<obs_ctext> (?&obs_NO_WS_CTL) )\n"
+ " (?<obs_domain> (?&atom) (?: \\. (?&atom) )*+ )\n"
+ " (?<obs_domain_list> \n"
+ " (?: (?&CFWS) | , )*+ \n"
+ " @ (?&domain) \n"
+ " (?: , (?&CFWS)? (?: @ (?&domain) )? )*+ \n"
+ " )\n"
+ " (?<obs_dtext> \n"
+ " (?&obs_NO_WS_CTL) | (?"ed_pair) \n"
+ " )\n"
+ " (?<obs_group_list> (?: (?&CFWS)? , )++ (?&CFWS)? )\n"
+ " (?<obs_local_part> (?&word) (?: \\. (?&word) )*+ )\n"
+ " (?<obs_mbox_list> \n"
+ " (?: (?&CFWS)? , )*+ \n"
+ " (?&mailbox) \n"
+ " (?: , \n"
+ " (?: (?&mailbox) | (?&CFWS))? \n"
+ " )*+ \n"
+ " )\n"
+ " (?<obs_phrase> \n"
+ " (?&word) \n"
+ " (?: (?&word) | \\. | (?&CFWS) )*+ \n"
+ " )\n"
+ " (?<obs_qp> \n"
+ " \\\\ \n"
+ " (?: \n"
+ " \\x{00} \n"
+ " | (?&obs_NO_WS_CTL) \n"
+ " | [\\x{0D}\\x{0A}] \n"
+ " ) \n"
+ " )\n"
+ " (?<obs_qtext> (?&obs_NO_WS_CTL) )\n"
+ " (?<obs_route> (?&obs_domain_list) : )\n"
+ " (?<phrase> (?&word)++ | (?&obs_phrase) )\n"
+ " (?<qcontent> (?&utf8_qcontent) )\n"
+ " (?<utf8_qcontent> \n"
+ " (?&utf8_qtext) \n"
+ " | (?&utf8_quoted_pair) \n"
+ " )\n"
+ " (?<qtext> [!\\#-\\[\\]-~] | (?&obs_qtext) )\n"
+ " (?<utf8_qtext> (?&qtext) | (?&UTF8_extra_char) )\n"
+ " (?<quoted_pair> \n"
+ " \\\\ (?&VCHARWSP) | (?&obs_qp) \n"
+ " )\n"
+ " (?<utf8_quoted_pair> \n"
+ " \\\\ (?&utf8_text) | (?&obs_qp) \n"
+ " )\n"
+ " (?<utf8_text> \n"
+ " [\\x{01}-\\x{09}\\x{0D}\\x{0A}\\x{0E}-\\x{7F}] \n"
+ " | (?&UTF8_extra_char) \n"
+ " )\n"
+ " (?<quoted_string> \n"
+ " (?&CFWS)? \" \n"
+ " (?: (?&FWS)? (?&qcontent) )*+ \n"
+ " (?&FWS)? \" (?&CFWS)? \n"
+ " )\n"
+ " (?<utf8_quoted_string> \n"
+ " (?&CFWS)? \" \n"
+ " (?: (?&FWS)? (?&utf8_qcontent) )*+ \n"
+ " (?&FWS)? \" (?&CFWS)? \n"
+ " )\n"
+ " (?<word> (?&utf8_atom) | (?&utf8_quoted_string) )\n"
+ ")";
final String string = "John <a@b.c>, w@com, \"wibb.[dhdhd]\"@(comment)fred.com";
final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
final Matcher matcher = pattern.matcher(string);
if (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