0

I am trying to split data from a .csv file, however, some of the fields/columns also contain commas in between just like this

ABCKS,"ASK,ED","SDR,ED",2022-07-11,8011.0
cvbgb,"hfhvnf,rgr","dthd,chdf",2022-07-11,111.9
ABCKS,"ASK,ED","SDR,ED",2022-07-11,8011.0

hence, the .split(",") string method would create additional fields into the data.

I have tried

if (aLine.contains("\"|,|\"")){

    String newString = aLine.replaceAll("\"|,|\"","|_|").replaceAll("\"", "");
    aList = Arrays.asList(newString.split(",", -1));

}

It does not seem to work.

Dada
  • 6,313
  • 7
  • 24
  • 43
  • 4
    You should use a proper CSV parser here which can handle this for you automatically. Using regex is possible, but prone to error and unforeseen edge cases. – Tim Biegeleisen Dec 13 '21 at 03:37

1 Answers1

1

As Tim said above, a proper CSV reader would probably be better but a simple solution could be something like this: ,(?![A-Za-z]+"). Where you select every comma that is not followed by letters and a quotation. This satisfies your sample data but if there are edge cases it can easily break.

Dzone64
  • 98
  • 7