$references = join '|', map {chomp; qr/\Q$_\E/} <$file_paths>; $regex = qr|\b($references)\b|; while (<>) { $_ =~ s/($regex)(.*)/$1\/$2/; print; }