$line = sprintf qq{Test #1 'Show me Waynes world','Jennys Basketball shoes'\n}; print $line; # <- added this line $line =~ s/(')[^'\\]*(?:\\.[^'\\]*)*(')/$1SSS$2/g; print $line; $line = sprintf qq{Test #2 'Show me Wayne\\'s world','Jenny\\'s Basketball shoes'\n}; # <- changed this line (two extra \ added) print $line; # <- added this line $line =~ s/(')[^'\\]*(?:\\.[^'\\]*)*(')/$1SSS$2/g; print $line;