$s = 'foo FOO Foo fOO'; $s =~ s/(foo)/$1 ^ lc($1) ^ "bar"/gie; print $s,"\n"; # outputs "bar BAR Bar bAR"