my $rx_srch = $replace_left_to_right ? qr{ .*? \K $srch }xms # search for leftmost match : qr{ .* \K $srch }xms # search for rightmost match ;