Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6065653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:24:51+00:00 2026-05-23T09:24:51+00:00

Possible Duplicate: How can I parse quoted CSV in Perl with a regex? I

  • 0

Possible Duplicate:
How can I parse quoted CSV in Perl with a regex?

I am attempting to take a CSV file and import each row into an array (where each element represents a column). The format of a CSV file is very simple:

item1,item2,item3
nextrowitem1,item2,item3
"items,with,commas","are,in,quotes"

I imported the CSV file using:

open(FILE, "test.csv");
@lines = <FILE>;

Then I looped through it using:

foreach(@lines){
    @items = split(/regular expression/);
    /*Do stuff with @items array*/
}

(Note that you do not need to use split(/regular expression, $string); because split() assumes $_ if no string is supplied)

Before I tested the file using a CSV file where none of the items contained commas and the simple regular expression of split(/,/). This worked just fine, so there is nothing wrong with the file, reading it, or my loop after this regular expression. However when I hit items that contained a comma they got understandably divided like so:

1 => "items
2 => with
3 => commas"
4 => "are
5 => in
6 => quotes"

Instead of the desired:

1 => items,with,commas
2 => are,in,quotes

Can anyone help me develop a regular expression to split this array correctly? Basically if the item starts with a quote ("), it needs to wait until "," to split. If the item does not start with a quote, it needs to wait until , to split.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:24:52+00:00Added an answer on May 23, 2026 at 9:24 am

    Try reading Text::CSV as a possible option that already does this. The problem with doing parsing of a CSV into a regular expression is that you have to look for things like "," (which you indicated) as well as just a , separation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Parsing XML in Cocoa iPhone - Can we parse .plist(xml) file using
Possible Duplicate: Parse query string into an array How can I explode a string
Possible Duplicate: How can I parse JSON in Google App Engine? json module was
Possible Duplicate: How can I convert my java program to an .exe file ?
Possible Duplicate: Parse query string into an array What's the fastest method , to
Possible Duplicate: What is the most efficient/elegant way to parse a flat table into
Possible Duplicate: Parse JSON with jQuery I tried to parse the following JSON File.
Possible Duplicate: Regex to parse youtube yid your probably thinking not another one, but
Possible Duplicate: Best methods to parse HTML How can I parse HTML code held
Possible Duplicate: Java API to parse XSD schema file I have a requirement to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.