Similar to yours and choroba's solutions but without string multiplication ('x'):
my $pchr = "KQRBNP";
my $w1 = "QRKPNBNBQRK";
my $w2 = "";
for my $L ( split //, $pchr ){
$w1 =~ m/$L(?{ $w2 .= $L })(*FAIL)/;
}
print "$w1 -> $w2\n"
And a bit similar to drclaw's idea:
my $pchr = "KQRBNP";
my $w1 = "QRKPNBNBQRK";
my $w2 = "";
my $re = join "|", map { ".*$_" } split //, $pchr;
$w1 =~ m/^(?:$re)(?<=(.))(?{ $w2 .= $1 })(*FAIL)/;
print "$w1 -> $w2\n"
Not sure if regex is optimized in some versions and this code fails. |