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 6980911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:05:16+00:00 2026-05-27T18:05:16+00:00

Found this code that breaks out CSV fields if contains double-quotes But I don’t

  • 0

Found this code that breaks out CSV fields if contains double-quotes
But I don’t really understand the pattern matching from regex

If someone can give me an step by step explanation of how this expression evaluates a pattern it would be appreciated

"([^\"]*)"|(?<=,|^)([^,]*)(?:,|$)

Thanks

====
Old posting

This is working well for me – either it matches on “two quotes and whatever is between them”, or “something between the start of the line or a comma and the end of the line or a comma”. Iterating through the matches gets me all the fields, even if they are empty. For instance,

the quick, “brown, fox jumps”, over, “the”,,”lazy dog” breaks down into

the quick “brown, fox jumps” over “the” “lazy dog”

import java.util.ArrayList; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class CSVParser { 

    /* 
     * This Pattern will match on either quoted text or text between commas, including 
     * whitespace, and accounting for beginning and end of line. 
     */ 
    private final Pattern csvPattern = Pattern.compile("\"([^\"]*)\"|(?<=,|^)([^,]*)(?:,|$)");   
    private ArrayList<String> allMatches = null;         
    private Matcher matcher = null; 
    private String match = null; 
    private int size; 

    public CSVParser() {                 
        allMatches = new ArrayList<String>(); 
        matcher = null; 
        match = null; 
    } 

    public String[] parse(String csvLine) { 
        matcher = csvPattern.matcher(csvLine); 
        allMatches.clear(); 
        String match; 
        while (matcher.find()) { 
                match = matcher.group(1); 
                if (match!=null) { 
                        allMatches.add(match); 
                } 
                else { 
                        allMatches.add(matcher.group(2)); 
                } 
        } 

        size = allMatches.size();                
        if (size > 0) { 
                return allMatches.toArray(new String[size]); 
        } 
        else { 
                return new String[0]; 
        }                        
    }    

    public static void main(String[] args) {             
        String lineinput = "the quick,\"brown, fox jumps\",over,\"the\",,\"lazy dog\""; 

        CSVParser myCSV = new CSVParser(); 
        System.out.println("Testing CSVParser with: \n " + lineinput); 
        for (String s : myCSV.parse(lineinput)) { 
                System.out.println(s); 
        } 
    } 

} 
  • 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-27T18:05:17+00:00Added an answer on May 27, 2026 at 6:05 pm

    I try to give you hints and the needed vocabulary to find very good explanations on regular-expressions.info

    "([^\"]*)"|(?<=,|^)([^,])(?:,|$)
    

    () is a group

    * is a quantifier

    If there is a ? right after the opening bracket then it’s a special group, here (?<=,|^) is a lookbehind assertion.

    Square brackets declare a character class e.g. [^\"]. This one is a special one, because of the ^ at the start. It is a negated character class.

    | denotes an alternation, i.e. an OR operator.

    (?:,|$) is a non capturing group

    $ is a special character in regex, it is an anchor (which matches the end of the string)

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

Sidebar

Related Questions

I've found this small code that sends email to gmail users. I'd like the
I found that this C++ code: vector<int> a; a.push_back(1); a.push_back(2); vector<int>::iterator it = a.begin();
This morning we found an old chunk of code that was causing a library
Edit: This code is fine. I found a logic bug somewhere that doesn't exist
I had posted a question in regards to this code. I found that JTextArea
While trying to answer this question I found that the code int* p =
I found some mapkit code on the internet that looks like this: - (void)recenterMap
I'm attempting to use code I've found that uses Win32. However, I'm getting this
This code is buggy but can't figure out why ... want to populate an
I am pretty sure I have seen this before, but I haven't found out

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.