while (<$IN>) { next if /^XYZ/ or /ZYX/ or /XZY/ or /YZZ$/; # removes all special cases where substitution should not occur s/foo/bar/g; # do something with $_ }