$text =~ s/(.*)($string_1)(.*)($string_2)(.*)/$1$4$3$2$5/ #taken from recent post if ($text =~ m/^.*(\.txt)$/) #used to see if $text ends in .txt