473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Perl expression for parsing CSV (ignoring parsing commas when in double quotes)

I can't figure an expression needed to parse a string.

This problem arrises from parsing Excel csv files ...

The expression must parse a string based upon comma delimiters,
but if a comma appears in double quotes it should not be used for parsing.

For example in the simple case we'd have :

$a='hello,brave,world';

($v,$x,$y) = split(/,/,$a);

Should yeild (no suprise here)

hello for $v
brave for $x, and
world for $y
For the more advanced case we'd have :

$a='hello,"brave, but cruel, and joyous","planet, or world"';

hello for $v
brave, but cruel, and joyous for $x, and
planet, or world for $y
Thanks in advance to the kind souls who are sharp enough to get this one.
Jul 19 '05 #1
2 17925
GIMME wrote:
I can't figure an expression needed to parse a string.
Thanks in advance to the kind souls who are sharp enough to get this one.


#!/bin/perl
use Text::CSV;
my $a = 'hello,"brave, but cruel, and joyous","planet, or world"';
my $csv = Text::CSV->new();
my $status = $csv->parse($a);
my @fields = $csv->fields();
print "a=$a status=$status fields=\n\t", join("\n\t",@fields), "\n";

-Joe
Jul 19 '05 #2
Thanks very much Joe.

I didn't say it but I found a post which easily translated
to Java and ORO ... Next time I'll look before posting.
Haha haha.

This

while($_){
s/([^,]*?(".*?")*)(,|$)//;
push @out, $1;
}

Became this ...

import org.apache.oro.text.perl.Perl5Util;

public String[] parseRecordString(String record) {
ArrayList r = new ArrayList();
while(! record.equals("") ) {
record = P5UTIL.substitute("s/([^,]*?(\".*?\")*)(,|$)//",record);
r.add(P5UTIL.group(1));
}
return (String[]) r.toArray(new String[r.size()]);
}
Joe Smith <Jo*******@inwap.com> wrote in message news:<zhjWb.3517
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Sims | last post by:
Hi, I need some help to split data using regular expression Consider the string '1,2,3', I can split it using, preg_split("/,/", '1,2,3') and i correctly get =1, =2,=3. Now if i have
4
by: M Wells | last post by:
Hi All, I couldn't find a regular expressions group to ask this in, so I thought I'd ask here as I'm a little familiar with php's regular expressions syntax. I have a comma delimited text...
0
by: Nut Shell | last post by:
I needed to parse a csv file for my project. Could not find any so I put together this code. It works for general cases. Use it and let me know. Just Cut and paste the 2 functions in a new Form...
2
by: scadav | last post by:
I am new to Perl and I am trying for figure out how to solve this problem. If anyone can give me some suggestions, I would greatly appreciate it. I am trying to read a log file and generate...
19
by: ARK | last post by:
I am writing a search program in ASP(VBScript). The user can enter keywords and press submit. The user can separate the keywords by spaces and/or commas and key words may contain plain words,...
4
by: Alan Lane | last post by:
Hello world: I have a ListBox that I fill as a ValueList from a SQL query. The SQL looks like: SELECT Format(COST,"$#,000") As ItemCost FROM tblPricing ... I put semicolons between each...
3
by: Mad Scientist Jr | last post by:
i am trying to validate a field for a double, but not allow commas the regex specifies any number of whole number digits * no comma * an optional...
5
by: Cylix | last post by:
I am going to write a function that the search engine done. in search engine, we may using double quotation to specify a pharse like "I love you", How can I using regular expression to sperate...
5
by: Looch | last post by:
All, Looking for a regex expression to split this comma delimted string ultimately into 51 fields. The fields in double quotes can have commas within the quotes (separating values within the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.