import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?<timestamp>[^ ]*)\\.(?<time_microseconds>\\d*)\\+[^ ]+ (?<host>[^ ]*) postgres\\[(?<pid>\\d*)\\]: \\[(?<groupid>[^ ]\\d*)-(?<lineid>[^ ]\\d*)\\](?<message>.*)$";
final String string = "2017-03-21T03:01:32.627434+00:00 ec317 postgres[82271]: [134-1] 2017-03-21 03:01:32 UTC [82271]: [2-1] user=unknown db=[unknown] LOG: incomplete startup packet\n"
+ "2017-03-21T03:01:32.761669+00:00 ec317 postgres[82270]: [135-1] 2017-03-21 03:01:32 UTC [82270]: [3-1] user=gz_kettle,db=greenzone LOG: disconnection: session time: 0:00:00.182 user=gz_kettle database=greenzone host=172.26.27.22 port=35849\n"
+ "2017-03-21T03:01:32.800403+00:00 ec317 postgres[82272]: [133-1] 2017-03-21 03:01:32 UTC [82272]: [1-1] user=[unknown],db=[unknown] LOG: connection received: host=172.26.27.22 port=35850\n"
+ "2017-03-21T03:01:32.801596+00:00 ec317 postgres[82272]: [134-1] 2017-03-21 03:01:32 UTC [82272]: [2-1] user=gz_kettle,db=greenzone LOG: connection authorized: user=gz_kettle database=greenzone";
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