In perl, how can I split a string along only unquoted delimiters? i.e. the following string:
my $line = '"a quoted, comma", word1, word2';
should result in an array with the elements:
"a quoted, comma"
word1
word2
In perl, how can I split a string along only unquoted delimiters? i.e. the following string:
my $line = '"a quoted, comma", word1, word2';
should result in an array with the elements:
"a quoted, comma"
word1
word2
You can use parse_line()
of Text::ParseWords.
use Text::ParseWords;
my $line = '"a quoted, comma", word1, word2';
my @parsed = parse_line(',', 1, $line);
# print "@parsed\n"; # this will print in single line
# To print in new line
foreach (@parsed)
{
print "$_\n";
}
Output:
"a quoted, comma"
word1
word2
You can do this with a simple alternate regex pattern
use strict;
use warnings;
use 5.010;
my $line = q<"a quoted, comma", word1, word2>;
my @words = $line =~ / (?: "[^"]*" | [^,] )+ /xg;
say for @words;
"a quoted, comma"
word1
word2