Solved

How to split columns with exceptions

Posted on 2008-10-13
5
461 Views
Last Modified: 2012-05-05
Hi,
   Can anyone help me with this problem:

How can I split CSV file in Java (into separate columns), using built-in regex functions (for efficiency), but ignore everything in double quotes? So:

1,2,3,"hello, world!"

will be treated as 4 columns, not 5.
0
Comment
Question by:Envoy2064
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 22705134
If you won't have empty columns you could do something like:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
  public static void main(String[] asd){
  String sourcestring = "source string to match with pattern";
  Pattern re = Pattern.compile("""[^""]*""|[^,]+");
  Matcher m = re.matcher(sourcestring);
    if(m.find()){
      for( int groupIdx = 0; groupIdx < m.groupCount(); groupIdx++ ){
        System.out.println( "[" + groupIdx + "] = " + m.group(groupIdx));
      }
    }
  }
}

Open in new window

0
 

Author Comment

by:Envoy2064
ID: 22705155
How about with empty columns?
0
 

Author Comment

by:Envoy2064
ID: 22705173
Please note the emphasis on efficient algorithms that uses as much system-optimized code as possible.
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 250 total points
ID: 22734320
For that requirement (remember that you will need to split the file by line and pass each line to the regex function to get the column values):
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
  public static void main(String[] asd){
  String sourcestring = "source string to match with pattern";
  Pattern re = Pattern.compile("""[^""]*""|[^,]+|(?<=,)(?=,)|^(?=,)|(?<=,)$");
  Matcher m = re.matcher(sourcestring);
  Int mIdx = 0;
    while (m.find()){
      for( int groupIdx = 0; groupIdx < m.groupCount(); groupIdx++ ){
        System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
      }
      mIdx++;
    }
  }
}

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22779434
Thanks for the question and the points.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now