Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: split on comma-separated fields, where a field may have commas inside quotes

by Roy Johnson (Monsignor)
on Jun 10, 2009 at 21:00 UTC ( [id://770453]=note: print w/replies, xml ) Need Help??


in reply to Re: split on comma-separated fields, where a field may have commas inside quotes
in thread split on comma-separated fields, where a field may have commas inside quotes

This sub-thread effectively underscores why it's such a Good Idea to use modules. There are numerous edge cases that make this a difficult problem. Chief among them: what about empty fields? Certainly they should be possible, so you can't just grep them out. But you don't want to introduce them, either.

Then there's the issue about quoting quotes, which wasn't mentioned in this problem, but would probably come up eventually in any real-world case that gets used much. And reporting errors on malformed lines.

That said, I've got a regex that at least seems to deal with the empty fields properly:

/(?:^|,)((?:"[^"]*"|[^",]?)+)/g

Caution: Contents may have been coded under pressure.
  • Comment on Re^2: split on comma-separated fields, where a field may have commas inside quotes
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://770453]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found