import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "to=<(?P<email>\\S+)>,\\srelay=(?!10\\.10\\.10\\.).+dsn=(?P<dsn>\\d+\\.\\d+\\.\\d+),\\sstatus=(?!sent)";
final String string = "2016-10-17T00:00:00.028777+03:00 postfix/smtp[6628]: 878A13DCA5: to=<govorok@gmail.com>, relay=gmail-smtp-in.l.google.com[173.194.220.27]:25, delay=0.47, delays=0.01/0/0.12/0.35, dsn=2.0.0, status=sent (250 2.0.0 OK 1476651600 79si16679285lja.36 - gsmtp)\n"
+ "2016-10-17T00:00:00.029273+03:00 postfix/qmgr[4674]: 878A13DCA5: removed\n"
+ "2016-10-17T00:00:00.037122+03:00 postfix/smtpd[6697]: 0906DA5: client=unknown[192.168.2.74]\n"
+ "2016-10-17T00:00:00.039621+03:00 postfix/cleanup[6713]: 0906DA5: message-id=<20161016210000.0906DA5@mrelay-h3.hh.ru>\n"
+ "2016-10-17T00:00:00.040403+03:00 postfix/smtp[6690]: C5670B9: to=<daler_rajabov@inbox.ru>, relay=mxs.mail.ru[217.69.139.150]:25, delay=0.23, delays=0.01/0/0.02/0.2, dsn=2.0.0, status=1s3ent (250 OK id=1bvsXH-0003Cb-Rp)";
final Pattern pattern = Pattern.compile(regex);
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