Front page | perl.beginners |
Postings from September 2009
AW: Regular expression help
Thread Previous
|
Thread Next
From:
Tariq Doukkali
Date:
September 10, 2009 03:34
Subject:
AW: Regular expression help
Message ID:
192B76E92530564BAAB9DD78BAE565B7412606FDBD@chexchange.autoform.com
Hi,
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";
Tariq
-----Ursprüngliche Nachricht-----
Von: Dave Tang [mailto:d.tang@imb.uq.edu.au]
Gesendet: Mittwoch, 26. August 2009 08:23
An: beginners@perl.org
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
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
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Thread Previous
|
Thread Next