if ( my ($new1, $new2, $new3) = $string =~ /(p1)(p2)(p3)/ ) { $new1 =~ s/this/that/; $new2 =~ s/this2/that2/; $new3 =~ s/this3/that3/; ... }