Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl split list on commas except when within brackets?
    primarykey
    data
    text
    <p>I have a database with a number of fields containing comma separated values. I need to split these fields in Perl, which is straightforward enough except that some of the values are followed by nested CSVs contained in brackets that I do not want to split.</p> <p>Example:</p> <pre><code>recycling, environmental science, interdisciplinary (e.g., consumerism, waste management, chemistry, toxicology, government policy, and ethics), consumer education </code></pre> <p>Splitting on ", " gives me:</p> <pre><code>recycling environmental science interdisciplinary (e.g. consumerism waste management chemistry toxicology government policy and ethics) consumer education </code></pre> <p>What I want is:</p> <pre><code>recycling environmental science interdisciplinary (e.g., consumerism, waste management, chemistry, toxicology, government policy, and ethics) consumer education </code></pre> <p>Can any Perl regex(perts) lend a hand?</p> <p>I have tried modifying a regex string I found in a similar SO <a href="https://stackoverflow.com/questions/1566035/how-can-perl-split-a-line-on-whitespace-except-when-the-whitespace-is-in-doubleq">post</a> which returns no results:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; my $s = q{recycling, environmental science, interdisciplinary (e.g., consumerism, waste management, chemistry, toxicology, government policy, and ethics), consumer education}; my @parts = $s =~ m{\A(\w+) ([0-9]) (\([^\(]+\)) (\w+) ([0-9]) ([0-9]{2})}; use Data::Dumper; print Dumper \@parts; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload