Posted In: Java Core, String

How to read and parse CSV file using String Pattern

Splitting a comma-separated string but ignoring commas in quotes
Parsing CSV input with a RegEx in java

package com.example.csv;
import org.junit.Test;

public class CvsParser3 {
	@Test
	public void patternParser1() throws Exception {
		System.out.println("\npatternParser1 started");
		try {
			String line = "\"3MIN,DIA\",EQ,14450,14900,868,\"INE470,A01017\"";
			String[] tokens = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)",
			        -1);
			for (String token : tokens) {
				System.out.println("Element=" + token);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Tags:

by , on August 29th, 2017

  • Categories