in reply to Regex: Case insensitive search but case sensitive replace

If you use the /i switch in combination with 's///', it will capture any case permutation of whatever it is you are replacing, and if you use that captured text, it will appear in the exact same case permutation as it was captured. This will work no matter what you are trying to capture.

while ( chomp( my $line = <DATA> ) ) { $line =~ s/(Fred)/"$1"/i; print "$^C root@who1# perl while ( my $line = <DATA> ) { $line =~ s/(Fred)/"$1"/i; print $line; } __DATA__ Hello, my name is FRED! fREd went to the store. Nobody was home, except for freD.


Hello, my name is "FRED"!
"fREd" went to the store.
Nobody was home, except for "freD".

You mention in a later post that you are looking for something more generic that will work for anything, not just names. Perhaps this is what you are looking for?

my $tag = 'jane'; while ( <DATA> ) { s/($tag)/##$1##/i; # you might also want a /g here as well. print; } __DATA__ - John is following jane - John is following Jane


- John is following ##jane##
- John is following ##Jane##