FAQ 4.31 How can I split a [character] delimited string except when inside [character]?


PerlFAQ Server

This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .


4.31: How can I split a [character] delimited string except when inside [character]?

Several modules can handle this sort of parsing--"Text::Balanced",
"Text::CSV", "Text::CSV_XS", and "Text::parseWords", among others.

Take the example case of trying to split a string that is
comma-separated into its different fields. You can't use "split(/,/)"
because you shouldn't split if the comma is inside quotes. For example,
take a data line like this:

SAR001,"","Cimetrix, Inc","Bob Smith","CAM",N,8,1,0,7,"Error, Core Dumped"

Due to the restriction of the quotes, this is a fairly complex problem.
Thankfully, we have Jeffrey Friedl, author of *Mastering Regular
Expressions*, to handle these for us. He suggests (assuming your string
is contained in $text):

@new = ();
push(@new, $+) while $text =~ m{
"([^\"\\]*(?:\\.[^\"\\]*)*)",? # groups the phrase inside the quotes
| ([^,]+),?
| ,
push(@new, undef) if substr($text,-1,1) eq ',';

If you want to represent quotation marks inside a
quotation-mark-delimited field, escape them with backslashes (eg, "like

Alternatively, the "Text::parseWords" module (part of the standard Perl
distribution) lets you say:

use Text::parseWords;
@new = quotewords(",", 0, $text);


The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question