1

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
serenesat
  • 4,611
  • 10
  • 37
  • 53
Spacemoose
  • 3,856
  • 1
  • 27
  • 48
  • Related: [How do I efficiently parse a CSV file in Perl?](http://stackoverflow.com/questions/3065095/how-do-i-efficiently-parse-a-csv-file-in-perl) – Michael Carman Jul 02 '15 at 17:57

2 Answers2

7

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
serenesat
  • 4,611
  • 10
  • 37
  • 53
  • Thanks for the help. Is there someway I can tell ParseWords to treat both ""'s and matched parentheses as quotes? E.g. (a,b) is a word? – Spacemoose Jul 02 '15 at 18:57
  • Dear downvoter, your comment will help me to improve my answer. Do not forget to tell us why did you downvote? :) – serenesat Dec 08 '17 at 09:05
2

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;

output

"a quoted, comma"
 word1
 word2
Borodin
  • 126,100
  • 9
  • 70
  • 144