use strict;
my $str = '/var/log/audit/audit.log
/var/log/audit/audit.log.1
/var/log/boot.log
/var/log/btmp
/var/log/btmp-20170401
/var/log/chrony
/var/log/cron
/var/log/cron-20170326
/var/log/cron-20170402
/var/log/cron-20170410
/var/log/cron-20170416
/var/log/anaconda
./var/log/anaconda/anaconda.log
./var/log/anaconda/ifcfg.log
./var/log/anaconda/journal.log
./var/log/anaconda/ks-script-XcavhD.log
./var/log/anaconda/packaging.log
./var/log/anaconda/program.log
./var/log/anaconda/storage.log
./var/log/anaconda/syslog
./var/log/anaconda/X.log
./var/log/audit
./var/log/audit/audit.log
./var/log/audit/audit.log.1
/var/log/httpd/error_log;
';
my $regex = qr/^.+\/(.+?)[\W|$]/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