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

Extracting the quoted strings and then searching for them again is kind of inefficient. Likewise for having to create a new regexp for every single quoted string. Perl goes to a fair amount of trouble to compile a regexp into something that walks through a string; the whole point of that is so that you can re-use that walk routine lots of times.

Meaning what you really want is a single regexp fixed in advance that gets applied to every line. Bonus points if you can have it be a walk-thingie that only has to do a single pass through the string (which then means you want to think about how you'd do the walk if you had to do it yourself). Try this

use strict; use warnings; while ( my $line = <DATA> ) { print "# ORIG: $line"; if ($line =~ m/"/g) { $line =~ s/\G([^,"]*(?:"[^"]*"[^",]*)*),/$1|/g; } print "# NEW: $line"; } __DATA__ ONE,"TWo,2",Three,Four,Five ONE,"$TWo,2",Three,Four,"Fi,ve,," ONE,"$TWo2","Three,(3)",Four,Five ONE,"$TWo2","Three,(3",Four,Five
  • Comment on Re: Search & Replace doesn't work with string containing certain special characters
  • Download Code