Front page | perl.beginners |
Postings from August 2009
Regular expression help
Thread Next
From:
Dave Tang
Date:
August 25, 2009 23:23
Subject:
Regular expression help
Message ID:
op.uy9ao4ak8cr96e@imb09-02635.imbpc.ad
Dear list,
I am trying to import entries in a csv file into a relational database,
however there are entries such as:
a,b,c,d,e,"f1,f2","g1,g2" which spoil my split(/,/).
The quotes group f1 and f2 as a single entry. I know each each database
entry should be atomic, but I'll deal with that later.
So I would like to change the above entry to
a,b,c,d,e,f1|f2,g1|g2
I had an inelegant solution:
while ($line =~ /(".*?")/g){
my $comma = my $noComma = $1;
$noComma =~ s/,/|/g;
$noComma =~ s/"//g;
$line =~ s/$comma/$noComma/;
}
But this had problems, when an entry is:
a,b,c,d,e,"f(1),f(2)","g(1),g(2)"
since the parentheses need to be escaped in the substitution. The data I'm
working with is highly unpredictable, as illustrated by the above example.
What is consistent are the commas and quotes.
I could probably come up with a solution but am afraid it will only become
more and more ugly.
Could someone provide some guidance?
Thanks!
Dave
Thread Next
-
Regular expression help
by Dave Tang