in reply to deleting commas enclosed by quotation marks
Tokenizer approach:
$input = '7374,726327,"76,237",32324,"21,342,857",23'; print("input: $input\n"); $output = ''; foreach ($input) { if (/\G("[^"]+")/gc) { my $quoted = $1; $quoted =~ s/,//g; $output .= $quoted; redo; } # Unmatched quote. if (/\G"/gc) { $output .= '"'; redo; } if (/\G([^"]+)/gc) { $output .= $1; redo; } } print("output: $output\n");
To avoid reinventing the wheel, I do recommed Text::CSV_XS or similar over my solution. Use it to seperated the fields, then remove the comma from every field with something like s/,//g foreach @fields;.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: deleting commas enclosed by quotation marks
by ambrus (Abbot) on Jun 22, 2005 at 08:56 UTC | |
by Fletch (Bishop) on Jun 22, 2005 at 23:59 UTC |