in reply to Need help with replace or substitute

as someone asked for code this is code snip... as whole code is quite big ..
#!c:/perl/bin/perl $string='"the, sample, string",345,43","A, next , string",12,-90'; print "$string\n"; #######$string=~ s/<need help here>/<need help here>/g ; print "$string\n";
output required:
"the, sample, string",345,43","A, next , string",12,-90 "the# sample# string",345,43","A# next # string",12,-90'

Replies are listed 'Best First'.
Re^2: Need help with replace or substitute
by runrig (Abbot) on Apr 11, 2008 at 23:31 UTC
    You might want to look at Text::xSV or Text::CSV or Text::CSV_XS to break up the fields. Then you can just s/,/#/g on each field, and print back out the results delimited by commas.

      Or with one line of regex code:

      $string =~ s/("[^"]*")/local $_=$1; s!,!#!g; $_ /ge;

      Yes it won't handle escaped double quotes but if you don't have that edge case it will never matter.

        the output with ur regex coming in this code snip as: "the, sample, string",345,43","A, next , string",12,-90 "the# sample# string",345,43"#"A, next , string",12,-90
        thanks this is much appreciated as we can't use any extra CPAN modules other than which are installed there due to some policy voilation.
      Hey Thanks, it worked for me.