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

AW: Regular expression help

Thread Previous | Thread Next
Tariq Doukkali
September 10, 2009 03:34
AW: Regular expression help
Message ID:

try with this code:

#!/usr/bin/perl -w

my $test = 'a,c,d,f,r,t,"f(3),g(4)",d,f,f,f,"f(3),t(8)",d,t';
$test =~ s/\"([a-z][\(0-9\)]*),([a-z][\(0-9\)]*)\"/$1|$2/g;
print $test . "\n";


-----Urspr√ľngliche Nachricht-----
Von: Dave Tang [] 
Gesendet: Mittwoch, 26. August 2009 08:23
Betreff: Regular expression help

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?



To unsubscribe, e-mail:
For additional commands, e-mail:

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