perldoc -q "delimited string"
Found in /usr/lib/perl5/5.8.8/pod/perlfaq4.pod
How can I split a [character] delimited string except when inside [character]?
Several modules can handle this sort of pasing---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
| ([^,]+),?
| ,
}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 back-
slashes (eg, "like \"this\"".
Alternatively, the Text:arseWords module (part of the standard Perl distribution) lets you say:
use Text:arseWords;
@new = quotewords(",", 0, $text);
There's also a Text::CSV (Comma-Separated Values) module on CPAN.
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |