简体   繁体   中英

Need a Java Regular Expression (regexp) to split a complex CSV file

I need to split a CSV file (which I read in as String) containing orders. Parsing rules of this CSV are not that complex:

Each order has several lines (no fix number)

Each order begins with one line (initiator):

111;222;dynamic content

Each order ends with two lines (terminator):

111;333;dynamic content
111;333;dynamic content

Initiator and terminator have to be included in the outcome of the split.

[EDIT] I do not need to use regexp. This was just my first idea. I could also use another solution, if it is easier. [/EDIT]

I have not much experience with regexp, so even getting started takes a long time :-( Eg, I tried the following:

String[] parts = body.split("111;333;.*111;333;");

Expected result: Splitter is 111;333; to 111;333; - remember, every order ends with two lines, both begin with 111;333; so, String[] should contain the correct number of orders IMO. However, there is only one String in the array which contains everything.

I played around with some other regexps, but I cannot get a good result. Can some one help me and tell me the regular expression for this split? Thanks...

Best regards, Kai

I really would discourage you to try this task using regex. There are many Java libraries for doing this for you. These libraries are tested and very well known. Just take any of them. You will save time and effort. I have good experience using opencsv .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM