import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = " (?(DEFINE)\n"
+ " (?<addr_spec> (?&local_part) @ (?&domain) )\n"
+ " (?<local_part> (?&dot_atom) | (?"ed_string) | (?&obs_local_part) )\n"
+ " (?<domain> (?&dot_atom) | (?&domain_literal) | (?&obs_domain) )\n"
+ " (?<domain_literal> (?&CFWS)? \\[ (?: (?&FWS)? (?&dtext) )* (?&FWS)? \\] (?&CFWS)? )\n"
+ " (?<dtext> [\\x21-\\x5a] | [\\x5e-\\x7e] | (?&obs_dtext) )\n"
+ " (?<quoted_pair> \\\\ (?: (?&VCHAR) | (?&WSP) ) | (?&obs_qp) )\n"
+ " (?<dot_atom> (?&CFWS)? (?&dot_atom_text) (?&CFWS)? )\n"
+ " (?<dot_atom_text> (?&atext) (?: \\. (?&atext) )* )\n"
+ " (?<atext> [a-zA-Z0-9!#$%&'*+\\/=?^_`\\{|\\}~-]+ )\n"
+ " (?<atom> (?&CFWS)? (?&atext) (?&CFWS)? )\n"
+ " (?<word> (?&atom) | (?"ed_string) )\n"
+ " (?<quoted_string> (?&CFWS)? \" (?: (?&FWS)? (?&qcontent) )* (?&FWS)? \" (?&CFWS)? )\n"
+ " (?<qcontent> (?&qtext) | (?"ed_pair) )\n"
+ " (?<qtext> \\x21 | [\\x23-\\x5b] | [\\x5d-\\x7e] | (?&obs_qtext) )\n\n"
+ " # comments and whitespace\n"
+ " (?<FWS> (?: (?&WSP)* \\r\\n )? (?&WSP)+ | (?&obs_FWS) )\n"
+ " (?<CFWS> (?: (?&FWS)? (?&comment) )+ (?&FWS)? | (?&FWS) )\n"
+ " (?<comment> \\( (?: (?&FWS)? (?&ccontent) )* (?&FWS)? \\) )\n"
+ " (?<ccontent> (?&ctext) | (?"ed_pair) | (?&comment) )\n"
+ " (?<ctext> [\\x21-\\x27] | [\\x2a-\\x5b] | [\\x5d-\\x7e] | (?&obs_ctext) )\n\n"
+ " # obsolete tokens\n"
+ " (?<obs_domain> (?&atom) (?: \\. (?&atom) )* )\n"
+ " (?<obs_local_part> (?&word) (?: \\. (?&word) )* )\n"
+ " (?<obs_dtext> (?&obs_NO_WS_CTL) | (?"ed_pair) )\n"
+ " (?<obs_qp> \\\\ (?: \\x00 | (?&obs_NO_WS_CTL) | \\n | \\r ) )\n"
+ " (?<obs_FWS> (?&WSP)+ (?: \\r\\n (?&WSP)+ )* )\n"
+ " (?<obs_ctext> (?&obs_NO_WS_CTL) )\n"
+ " (?<obs_qtext> (?&obs_NO_WS_CTL) )\n"
+ " (?<obs_NO_WS_CTL> [\\x01-\\x08] | \\x0b | \\x0c | [\\x0e-\\x1f] | \\x7f )\n\n"
+ " # character class definitions\n"
+ " (?<VCHAR> [\\x21-\\x7E] )\n"
+ " (?<WSP> [ \\t] )\n"
+ " )\n"
+ " ^(?&addr_spec)$";
final String string = "fdas@gad.com";
final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
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