$re = qr[ ( the(?{$rep='The'}) | quick(?{$rep='Quick'}) | brown(?{$rep='Brown'}) | fox(?{$rep='Fox'}) ) ]x; $s = 'the quick brown fox'; $s =~ s[$re][$rep]g; print $s; The Quick Brown Fox