foreach my $val (@nets_q) { $data =~ s/( [NOR|OR].*.IN1\()($val)(\).*IN2\()([^\)]*)(.*)/$1$4$3$2$5/g; }