in reply to Search & Replace doesn't work with string containing certain special characters

tr/// does not take character classes, only literal strings and character ranges.

You need to quote the search string in s/// ...

cat x.pl; echo ; perl x.pl use strict; use warnings; while ( my $line = <DATA> ) { my $old = $line; my @match = ( $line =~ m/ "( [^"]* )" /gx ) ; #" for my $str ( @match ) { ( my $replace = $str ) =~ tr/,/|/ ; # Search string quoted. $line =~ s/ \Q${str}\E /$replace/gx; } printf "ORIG: %s NEW: %s\n" , $old , $line; } __DATA__ ONE,"TWo,2",Three,Four,Five ONE,"$TWo,2",Three,Four,Five ONE,"$TWo2","Three,(3)",Four,Five ONE,"$TWo2","Three,(3",Four,Five ORIG: ONE,"TWo,2",Three,Four,Five NEW: ONE,"TWo|2",Three,Four,Five ORIG: ONE,"$TWo,2",Three,Four,Five NEW: ONE,"$TWo|2",Three,Four,Five ORIG: ONE,"$TWo2","Three,(3)",Four,Five NEW: ONE,"$TWo2","Three|(3)",Four,Five ORIG: ONE,"$TWo2","Three,(3",Four,Five NEW: ONE,"$TWo2","Three|(3",Four,Five

Replies are listed 'Best First'.
Re^2: Search & Replace doesn't work with string containing certain special characters
by mmbb (Initiate) on Sep 09, 2014 at 09:42 UTC
    It works!! Thank you so much!!!