my $pattern = qr/$test/; my $pattern1 = qr/$test1/; # ... $new =~ s/$pattern/$pattern1/;