develooper Front page | perl.beginners | Postings from August 2009

Regular expression help

Thread Next
Dave Tang
August 25, 2009 23:23
Regular expression help
Message ID:
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


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:


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?



Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About