Solved

How to split columns with exceptions

Posted on 2008-10-13
5
453 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

757 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

19 Experts available now in Live!

Get 1:1 Help Now