in reply to Add Quotes to entries in a file

FWIW, a single-regex solution:

c:\@Work\Perl\monks>perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; my @vectors = ( [ 'ABC;123;;;;;HELLO;' => q{ABC;'123';'';'';'';'';'HELLO';} ], [ 'DEF;345;;BANANA;12DEF;44,55;4*12;;;;;;;;3;' => q{DEF;'345';'';'BANANA';'12DEF';'44,55';'4*12';'';'';'';'';'';''; +'';'3';} ], ); ;; for my $ar_vector (@vectors) { my ($string, $expected) = @$ar_vector; (my $got = $string) =~ s{ (?<= ;) ([^;]*) (?= ;) }{'$1'}xmsg; is $got, $expected, qq{'$string' -> \n >$expected<}; } " ok 1 - 'ABC;123;;;;;HELLO;' -> # >ABC;'123';'';'';'';'';'HELLO';< ok 2 - 'DEF;345;;BANANA;12DEF;44,55;4*12;;;;;;;;3;' -> # >DEF;'345';'';'BANANA';'12DEF';'44,55';'4*12';'';'';'';'';'';'';''; +'3';< ok 3 - no warnings 1..3
Notes:

Update: The regex used above has also been tested and works not only with "pure" strings (i.e., with nothing after the final semicolon), but with test strings ending in a newline after the final semicolon, as would be presumed to be the case for strings read from a file.


Give a man a fish:  <%-{-{-{-<