use strict;
my $str = '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
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
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
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';
my $regex = qr/^(?<timestamp>[^ ]*)\.(?<time_microseconds>\d*)\+[^ ]+ (?<host>[^ ]*) postgres\[(?<pid>\d*)\]: \[(?<groupid>[^ ]\d*)-(?<lineid>[^ ]\d*)\](?<message>.*)$/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
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 Perl, please visit: http://perldoc.perl.org/perlre.html