use strict;
my $str = 'log-bucket/AWSLogs/113901497002/elasticloadbalancing/us-east-1/2020/04/26/113901497002_elasticloadbalancing_us-east-1_app.us-east-lt-alb.5ac1c221b7cf2245_20200426T0225Z_34.233.232.149_nhinzchd.log.gz
log-bucket/web-lt/AWSLogs/113901497002/elasticloadbalancing/us-east-1/2020/04/26/113901497002_elasticloadbalancing_us-east-1_app.us-east-lt-alb.5ac1c221b7cf2245_20200426T0225Z_34.233.232.149_nhinzchd.log.gz
';
my $regex = qr/^(?P<bucket>[^\/]+)\/(?:(?P<prefix>.+)\/)?AWSLogs\/(?P<aws_account_id>[\d]{12})\/elasticloadbalancing\/(?P<region>[\w\d\-]+)\/(?P<year>\d{4})\/(?P<month>\d{2})\/(?P<day>\d{2})\/(?P=aws_account_id)_elasticloadbalancing_(?P=region)_(?P<load_balancer_id>[\w\.\-\d]+)_(?P<end_time>\d{8}T\d{4}Z)_(?P<ip_address>(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))_(?P<random_string>.+)\.log\.gz$/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