in reply to Regex negation (or golf)

The format of the response is potentially difficult so some sort of parser would probably be the safest option. The possibility of commas in the quoted parts of the response makes splitting on comma unreliable. Assuming fields are delimited by commas and sub-fields are one inside the double-quotes and one after them, this should work.

use strict; use warnings; use Data::Dumper; my $rxFlds = qr {(?x) " ([^"]*) " ([^,]*) (?:,|\z) }; my $resp = q{"F059"3,"Invalid blech"99,"","This, that"33,""77,}; my @flds; while ( $resp =~ m{$rxFlds}g ) { push @flds, [$1, $2]; } print Data::Dumper->Dumpxs([\@flds], [qw{*flds}])

Here's the output

@flds = ( [ 'F059', '3' ], [ 'Invalid blech', '99' ], [ '', '' ], [ 'This, that', '33' ], [ '', '77' ] );

I hope this is of use.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Regex negation (or golf)
by bradcathey (Prior) on Feb 20, 2007 at 15:35 UTC

    Precisely on the comma thing. The only thing I'm sure about is that a quote begins the next data pair.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
      The other worry would be if your double-quoted field contained double quotes, eg $resp = q{"Wrong "name" given"88};. All bets would be off then and you'd probably have to go for a parser solution.

      Cheers,

      JohnGG