Regarding Java Split Command Parsing Csv File

后端 未结 2 1149
醉酒成梦
醉酒成梦 2020-12-11 22:42

I have a csv file in the below format.

H,\"TestItems_20100107.csv\",07/01/2010,20:00:00,\"TT1198\",\"MOBb\",\"AMD\",NEW,,

I require the spl

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-11 23:14

    There's nothing wrong with the regular expression. The problem is that split discards empty matches at the end:

    This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

    A workaround is to supply an argument greater than the number of columns you expect in your CSV file:

     String[] tokens = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", 99);
    

提交回复
热议问题