Skip to content

Instantly share code, notes, and snippets.

@TecSachinGupta
Created March 14, 2021 11:18
Show Gist options
  • Save TecSachinGupta/500ef6eaa58220f2730ae03e63942882 to your computer and use it in GitHub Desktop.
Save TecSachinGupta/500ef6eaa58220f2730ae03e63942882 to your computer and use it in GitHub Desktop.
import java.util.regex.Pattern;
import java.util.Arrays;
public class MyClass {
public static Pattern COMMAS = Pattern.compile(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"); //Pattern.compile("\\\"([^\\\\\\\"]*)\\\"|(?<=,|^)([^,]*)(?=,|$)");
public static void main(String args[]) {
String string = "149,\"Libby, McNeill & Libby\",215.4,4.2,file:/home/sachin/eclipse-workspace/datasets/fortune500/fortune500-1955.csv\n"
+ "155,Archer Daniels Midland,207.7,5.0,file:/home/sachin/eclipse-workspace/datasets/fortune500/fortune500-1955.csv\n"
+ "156,Motorola,205.2,7.6,file:/home/sachin/eclipse-workspace/datasets/fortune500/fortune500-1955.csv";
String[] lines = string.split("[\\n]");
for(String line: lines) {
String[] aa = COMMAS.split(line);
System.out.println(Arrays.asList(aa));
}
//System.out.println("Sum of x+y = " + z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment