Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > FAQ 4.31 How can I split a [character] delimited string except when inside [character]?

Thread Tools

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

PerlFAQ Server
Posts: n/a
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 .


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:arseWords", 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:arseWords" module (part of the standard Perl
distribution) lets you say:

use Text:arseWords;
@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
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FAQ 4.31 How can I split a [character] delimited string except when inside [character]? PerlFAQ Server Perl Misc 0 01-25-2011 05:00 AM
convert non-delimited to delimited RyanL Python 6 08-28-2007 12:06 AM
split a tab delimited string Ajit ASP .Net 6 07-29-2003 03:32 PM