Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.perl.misc

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2009, 10:00 AM
PerlFAQ Server
Guest
 
Posts: n/a
Default FAQ 4.31 How can I split a [character] delimited string except when inside [character]?

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
| ([^,]+),?
| ,
}gx;
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
\"this\"".

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
perlfaq.pod.
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

Reply

Popular Tags in the Forum
431, character, delimited, faq, inside, split, string

Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the path and namefile in run time Pablo Newsgroup comp.lang.ada 5 10-06-2009 10:06 PM
containers.h jacob navia Newsgroup comp.lang.c 0 10-04-2009 05:38 PM
Re: preserve case using CALL PRXCHANGE Guido T Newsgroup comp.soft-sys.sas 0 07-29-2005 10:42 AM
Re: preserve case using CALL PRXCHANGE Chang Chung Newsgroup comp.soft-sys.sas 0 07-28-2005 11:35 PM
Re: preserve case using CALL PRXCHANGE Venky Chakravarthy Newsgroup comp.soft-sys.sas 0 07-28-2005 11:10 PM



All times are GMT. The time now is 04:29 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.