use strict;
my $str = 'DM1_65_78109
SL1_78_72947
Input is DM1_813_1881
Input is DM2_793_1808
Input is DM1_SL1_609_604980
output should be 813_1881
output 609_604980
output 793_1808';
my $regex = qr/(?<=SL{1}\d_)[0-9_]*|(?<=DM{1}\d\_)[0-9_]*/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