my $string = "...."; my $regex = qr/...(...).../; # must have $1 be the thing we're optimizing to reduce my @solutions; while ($string =~ g/(?=$regex)/g) { push @solutions, [[@-], [@+]]; # push starts and ends } # now walk through the solutions, and pick the one with the minimal span on $1 # [ code unfinished ]