需要Java正则表达式(regexp)来拆分复杂的CSV文件 [英] Need a Java Regular Expression (regexp) to split a complex CSV file

查看:122
本文介绍了需要Java正则表达式(regexp)来拆分复杂的CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要分割一个包含订单的CSV文件(我以String形式读取)。此CSV的解析规则并不那么复杂:

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]

我对regexp没有太多经验,所以即使入门也需要很长时间:-(
例如,我尝试过以下内容:

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

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

预期结果:分割器为111; 333;至111; 333;-请记住,每个订单都以两行都以111; 333开头;因此,String []应该包含正确数量的IMO订单。但是,数组中只有一个包含所有内容的String。

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...

最好的问候,
Kai

Best regards, Kai

推荐答案

我真的不鼓励您使用正则表达式尝试此任务。许多Java库可以帮助您完成这些操作。这些库已经过测试并且非常知名。只需使用其中的任何一个即可。时间和精力。我在使用 opencsv 方面有很好的经验。

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.

这篇关于需要Java正则表达式(regexp)来拆分复杂的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆