As has already been stated, Text::CSV is the most robust solutionf or this type or problem:
use Text::CSV; use strict; use warnings; my $inputstring = qq{field1\tfield2\t\"field3\t\t\"\tfield4\n}; my $csv = Text::CSV->new({eol => "\n", sep_char => "\t", binary => 1}) +; $csv->parse($inputstring) or die "csv error: " . $csv->error_input(); my @columns = $csv->fields(); s/\t//g for (@columns); $csv->combine(@columns) or die "csv error: " . $csv->error_input(); my $string = $csv->string(); print $string;
Here's the regex solution though using double quotes that allow escaped quotes
my $inputstring = qq{field1\tfield2\t\"field3\t\t\"\tfield4\n}; $inputstring =~ s{("(?: (?> [^\"\\]+ ) | \\ . )*")}{ (my $str = $1) =~ + s/\t//g; $str }xesg; print $inputstring;
In reply to Re: Remove tabs only between quotation marks
by wind
in thread Remove tabs only between quotation marks
by bestresearch2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |