in reply to Pattern matching
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 | |
by perlCrazy (Monk) on Feb 01, 2013 at 10:35 UTC | |
by tmharish (Friar) on Feb 01, 2013 at 10:50 UTC |