use strict; use warnings; my $renames = { this_sub => 'that_sub', var_1 => 'var_a', var_2 => 'var_b', Ignore => 'FAILED', nothing => 'FAILED', and => 'FILED', can => 'FAILED', t => 'FAILED', do => 'FAILED', it => 'FAILED', }; my $line = qq/ this_sub("first", var_1, "Ignore ""can't""", var_2) ' do it\n/; print $line; my $matched_keys = '\b' . join( '|', keys %$renames ) . '\b'; my $match = qr/($matched_keys)/; my $vb_string = qr/("[^"]*(?:""[^"]*)*")/; my @parts = split $vb_string, $line; my $do_code = 0; for my $part (@parts) { $do_code = !$do_code; next unless $do_code; if ( $part =~ m/'/ ) { my ( $code, $comment ) = split /'/, $part, 2; $code =~ s/$match/$renames->{$1}/ge; $part = "$code'$comment"; last; } else { $part =~ s/$match/$renames->{$1}/ge; } } print join( "", @parts )