use strict; use Switch; my $text="bla fred wilma fuck fred fuck wilma bla fred fredfred"; $text =~ s{(fred|wilma)} { my $replace_text; if($1 eq "fred") { $replace_text='wilma'; } else { $replace_text='fred'; }; $replace_text; }eg; print $text; #### use strict; use Switch; my $text="bla fred wilma fuck fred fuck wilma bla fred fredfred"; $text =~ s{(fred|wilma)} { my $replace_text; switch($1) { case 'fred' {$replace_text='wilma'} case 'wilma' {$replace_text='fred'} }; $replace_text; }eg; print $text;