>
>DB4515C,625.25,378,327,382,352,805,163,513.5,699,257.88,,,"4,503","1,801",805
>
>Trying to create a regex to substitute comas with pipes, except for the
>commas between the double quotes.

I think there is no regex which makes such a miracle. Maybe this has to do with the
fact that regex are related to context-free languages (in language theory terms)
and this kind of problem demands context-dependency (this is what gives comma
inside and outside quotes different meanings). But I am not absolutely right about this
and besides, Perl regexes are full with extensions which probably make it
able to recognize more complex languages. But this is just philosophical speculation.

I've tried to do something like this a little time ago,
struggling with some old programs which have not been properly built.

split won't don't it also because it cannot guess if it is inside or outside quotes.
And split relies on regexes. But maybe you can try to split with a special algorithm
and then make a join. For example, the following works.

# this splits a delimited string of unquoted fields and quoted fields
# delimiter is ',' and quotes are '"'
sub specialsplit {
    my ($string) = @_;
    my @array = ();
    while ($string =~ s/(".*?"|[^,]*),//) {
        push @array, $1;
    }
    push @array, $string unless ($string =~ /,$/);
    return @array;
}

And then with

        my $sample = 
q{DB4515C,625.25,378,327,382,352,805,163,513.5,699,257.88,,,"4,503","1,801",805};

        print join "|", specialsplit($sample);

you get what you asked for.

You'll have problems if you are to encounter quotes inside quotes.

(This is just an ugly solution. A more proficient Perl programmer would probably solve
it with more elegance.)

Hope this helps.

Adriano.




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to