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

In reply to Re: Search & Replace doesn't work with string containing certain special characters by wrog
in thread Search & Replace doesn't work with string containing certain special characters by mmbb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.