for my $line (@lines) { # break the line into an array of words minus exclusions my @words = grep{ !(1+index($excluded,$_)); } $line =~ m/\b\w+\b/g # Word breaks to avoid partial word matches local $local $"='\b|\b'; #" # replace each line with a multi-matching compiled regex $line = qr"\b@a\b"oi; }