import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "([a-zA-Z]+\\s+[\\d:]+\\s[\\d:]+)\\s(\\S+)\\s(\\S+):\\s([\\d]+-[\\d]+-[\\d]+\\s[\\d]+:[\\d]+:[\\d]+)?\\s?(.*)";
final String string = "Apr 15 10:34:05 scisp1 couriertcpd: Connection, ip=[::ffff:10.224.0.141]\n"
+ "Apr 15 10:34:05 scisp1 couriertcpd: LOGIN: ip=[::ffff:10.224.0.141], command=LOGIN\n"
+ "Apr 15 10:34:05 scisp1 couriertcpd: LOGIN: ip=[::ffff:10.224.0.141], username=61881227312\n"
+ "Apr 15 10:34:05 scisp1 couriertcpd: LOGIN, user=61881227312, ip=[::ffff:10.224.0.141], port=[59372], protocol=IMAP\n"
+ "Apr 15 10:34:51 scisp1 couriertcpd: TIMEOUT, user=61261886140, ip=[::ffff:10.224.0.141], headers=0, body=0, rcvd=747751, sent=43459, time=1835\n"
+ "Apr 15 10:34:55 scisp1 couriertcpd: TIMEOUT, user=61881226690, ip=[::ffff:10.224.0.141], headers=0, body=0, rcvd=242597, sent=10564, time=1812\n"
+ "Apr 15 10:34:55 scisp1 couriertcpd: LOGOUT, user=61881227312, ip=[::ffff:10.224.0.141], headers=0, body=438023, rcvd=1300, sent=447936, time=50\n"
+ "Apr 15 10:35:04 scisp1 exim[24212]: 2016-04-15 10:35:04 no IP address found for host 103-55-116-16.sipcity.com.au (during SMTP connection from [103.55.116.16])\n"
+ "Apr 15 10:35:04 scisp1 exim[24212]: 2016-04-15 10:35:04 1aqriy-0006IW-8u <= support@sipsolutions.com.au H=(scvm1.sipcity.com.au) [103.55.116.16] P=esmtps X=TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128 S=34149 id=Asterisk-4-132550271-61385471055-990@scvm1\n"
+ "Apr 15 10:35:06 scisp1 exim[24213]: 2016-04-15 10:35:06 1aqriy-0006IW-8u => bp@grindalpatrick.com.au R=dnslookup T=remote_smtp H=filter1.grinda-1.mailguard.com.au [103.248.191.202] X=TLS1.0:RSA_AES_128_CBC_SHA1:128 C=\"250 2.0.0 Ok: queued as C2CB94261A3 UID 571037396f7ee5bc\"\n"
+ "Apr 15 10:35:06 scisp1 exim[24213]: 2016-04-15 10:35:06 1aqriy-0006IW-8u Completed\n"
+ "Apr 15 10:35:08 scisp1 exim[24231]: 2016-04-15 10:35:08 no IP address found for host 103-55-116-18.sipcity.com.au (during SMTP connection from [103.55.116.18])\n"
+ "Apr 15 10:35:08 scisp1 exim[24231]: 2016-04-15 10:35:08 1aqrj2-0006Ip-7E <= customerservice@sipcity.com.au H=(scfax1.sipcity.com.au) [103.55.116.18] P=esmtps X=TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128 S=250689 id=20160415003508.GA29682@scfax1.sipcity.com.au\n"
+ "Apr 15 10:35:10 scisp1 exim[24232]: 2016-04-15 10:35:10 1aqrj2-0006Ip-7E => admin@northernurology.com.au R=dnslookup T=remote_smtp H=remote.northernurology.com.au [58.108.247.139] X=TLS1.0:ECDHE_RSA_AES_256_CBC_SHA1:256 C=\"250 2.6.0 <20160415003508.GA29682@scfax1.sipcity.com.au> [InternalId=9647] Queued mail for delivery\"\n"
+ "Apr 15 10:35:10 scisp1 exim[24232]: 2016-04-15 10:35:10 1aqrj2-0006Ip-7E -> darcy@northernurology.com.au R=dnslookup T=remote_smtp H=remote.northernurology.com.au [58.108.247.139] X=TLS1.0:ECDHE_RSA_AES_256_CBC_SHA1:256 C=\"250 2.6.0 <20160415003508.GA29682@scfax1.sipcity.com.au> [InternalId=9647] Queued mail for delivery\"\n"
+ "Apr 15 10:35:13 scisp1 exim[24232]: 2016-04-15 10:35:13 1aqrj2-0006Ip-7E => suki01@gmail.com R=dnslookup T=remote_smtp H=gmail-smtp-in.l.google.com [74.125.68.26] X=TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128 C=\"250 2.0.0 OK 1460680513 fl6si10358337pad.46 - gsmtp\"\n"
+ "Apr 15 10:35:14 scisp1 exim[24232]: 2016-04-15 10:35:14 1aqrj2-0006Ip-7E => admin@coralseaent.com.au R=dnslookup T=remote_smtp H=aspmx.l.google.com [74.125.68.27] X=TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128 C=\"250 2.0.0 OK 1460680514 vz3si1300160pab.93 - gsmtp\"\n"
+ "Apr 15 10:35:14 scisp1 exim[24232]: 2016-04-15 10:35:14 1aqrj2-0006Ip-7E Completed";
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