#!/usr/bin/perl # http://perlmonks.org/?node_id=1142266 use strict; use warnings; my $path = "XX.YY.ZZ"; my $s = "(ABC | (~dEf & hIJ)) & ~LMn if foo while bar"; my %ignore = map { $_, $_ } qw( if while for ); (my $answer = $s) =~ s/(\w+)/$ignore{$1} || "$path.$1"/ge; print "$answer\n";