use strict; use warnings; use Switch; my $text = 'bla fred wilma fuck fred fuck wilma bla fred fredfred'; sub replace_it { switch( $_[0] ) { case 'fred' { return 'wilma' } case 'wilma' { return 'fred' } die; }; } $text =~ s{(fred|wilma)}{replace_it($1)}eg; print $text;