import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(\\w{3} \\d{2} \\d{2}:\\d{2}:\\d{2}) ([a-z]+) ([\\w\\d\\[\\]]+): (.*)$";
final String string = "Jul 28 06:26:52 debian tripwire[29710]: Integrity Check Failed: File could not be opened.\n"
+ "Jun 29 06:54:01 debian kernel: [586133.428279] device eth0 left promiscuous mode\n"
+ "Jul 29 09:29:07 debian kernel: [586139.564278] device eth0 entered promiscuous mode\n"
+ "Dec 14 17:30:45 debian Cacti[14619]: POLLER: WARNING: Cron is out of sync with the Poller Interval!\n"
+ "Jan 29 11:48:59 debian Cacti[14626]: CMDPHP: Host[1] DS[29] WARNING: Result from SNMP not valid.\n"
+ "END of 00:00:00 logging - rotation in progress\n";
final Pattern pattern = Pattern.compile(regex, 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