in reply to Pattern matching

If you WANT to use RegEx then:
use strict; use warnings ; my @csv = <DATA>; foreach my $line ( @csv ) { my $clean_line = $line ; while( $line =~ /"(.*?)"/g ) { my $match = $1 ; my $match_cleaned = $match ; $match_cleaned =~ s/,/~/g; my $location = index( $line, $match ) ; my $clean_line = substr( $clean_line, $location, length( $match ), + $match_cleaned ); } print "$clean_line"; } __DATA__ 123,abc1,test1,"1,3,5",new,"abc,dfg,hj" 124,abc2,test2,"1,3,5",new,"abc,dfg,hj" 125,abc3*,test3,"*|1,3,5",new,"abc,dfg,hj"

Notice that replace withing $line is not done using a regex - this is to avoid escaping $match and to also speed things up.

You can compress this code but its been stretched out for readability.

Replies are listed 'Best First'.
Re^2: Pattern matching
by choroba (Cardinal) on Feb 01, 2013 at 10:30 UTC
    Yes, and pray for \" not appearing in the data.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks for help and input. below code works fine:
      my $file = "t.txt"; open(FH,$file) || die "can't open file \n"; while (<FH>) { chomp; my $line = $_; $line =~ s/\"(.*?)\"/replace($1)/ge; $line =~ s/\,/~/g; $line =~ s/\;/,/g; print "$line \n"; } close FH; sub replace { my ($str) = @_; $str =~ s/\,/\;/g; return $str; }
        perlCrazy like choroba has mentioned its easy to miss certain cases - Best to use a module that's been put through its paces on all kinds of different data.