Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java中的正则表达式和'|';分隔器_Java_Regex_Parsing_Csv - Fatal编程技术网

使用java中的正则表达式和'|';分隔器

使用java中的正则表达式和'|';分隔器,java,regex,parsing,csv,Java,Regex,Parsing,Csv,有人能帮我学正则表达式吗? 我有一个Java程序,它读入.csv文件将其加载到数据库中 目前ist使用的是Pattern csvPattern=Pattern.compile(\\\s*(\“[^\”]*\”[124;[^\]*)\\ s*,?”; 但是,我确实需要matcher=csvPattern.matcher(line);要逐行读取文件,我只得到空值。 这些文件的格式如下(许多行,有些行中带有逗号,“|”作为分隔符,并在每行末尾): 第一个文件的摘要: 0|ALGERIA|0| hagg

有人能帮我学正则表达式吗? 我有一个Java程序,它读入.csv文件将其加载到数据库中

目前ist使用的是
Pattern csvPattern=Pattern.compile(\\\s*(\“[^\”]*\”[124;[^\]*)\\ s*,?”;

但是,我确实需要
matcher=csvPattern.matcher(line);
要逐行读取文件,我只得到空值。 这些文件的格式如下(许多行,有些行中带有逗号,“|”作为分隔符,并在每行末尾):

第一个文件的摘要:

0|ALGERIA|0| haggle. carefully final deposits detect slyly agai|
1|ARGENTINA|1|al foxes promise slyly according to the regular accounts. bold requests alon|
2|BRAZIL|1|y alongside of the pending deposits. carefully special packages are about the ironic forges. slyly special |
第二:

|Customer#000000001|IVhzIApeRb ot,c,E|15|25-989-741-2988|711.56|BUILDING|to the even, regular platelets. regular, ironic epitaphs nag e|
2|Customer#000000002|XSTf4,NCwDVaWNe6tEgvwfmRchLXak|13|23-768-687-3665|121.65|AUTOMOBILE|l accounts. blithely ironic theodolites integrate boldly: caref|
3|Customer#000000003|MG9kdTD2WBHm|1|11-719-748-3364|7498.12|AUTOMOBILE| deposits eat slyly ironic, even instructions. express foxes detect slyly. blithely even accounts abov|
4|Customer#000000004|XxVSJsLAGtn|4|14-128-190-5944|2866.83|MACHINERY| requests. final, regular ideas sleep final accou|
第三:

5|Supplier#000000005|Gcdm2rJRzl5qlTVzc|11|21-151-690-3663|-283.84|. slyly regular pinto bea|
6|Supplier#000000006|tQxuVm7s7CnK|14|24-696-997-4969|1365.79|final accounts. regular dolphins use against the furiously ironic decoys. |
7|Supplier#000000007|s,4TicNGB4uO6PaSqNBUq|23|33-990-965-2201|6820.35|s unwind silently furiously regular courts. final requests are deposits. requests wake quietly blit|
8|Supplier#000000008|9Sq4bBH2FQEmaFOocY45sRTxo6yuoG|17|27-498-742-3860|7627.85|al pinto beans. asymptotes haggl|
9|Supplier#000000009|1KhUgZegwM3ua7dsYmekYBsK|10|20-403-398-8662|5302.37|s. unusual, even requests along the furiously regular pac|
第四:

1|2|3325|771.64|, even theodolites. regular, final theodolites eat after the carefully pending foxes. furiously regular deposits sleep slyly. carefully bold realms above the ironic dependencies haggle careful|
1|2502|8076|993.49|ven ideas. quickly even packages print. pending multipliers must have to are fluff|
1|5002|3956|337.09|after the fluffily ironic deposits? blithely special dependencies integrate furiously even excuses. blithely silent theodolites could have to haggle pending, express requests; fu|
1|7502|4069|357.84|al, regular dependencies serve carefully after the quickly final pinto beans. furiously even deposits sleep quickly final, silent pinto beans. fluffily reg|
第五:

1|155190|7706|1|17|21168.23|0.04|0.02|N|O|1996-03-13|1996-02-12|1996-03-22|DELIVER IN PERSON|TRUCK|egular courts above the|
1|67310|7311|2|36|45983.16|0.09|0.06|N|O|1996-04-12|1996-02-28|1996-04-20|TAKE BACK RETURN|MAIL|ly final dependencies: slyly bold |
第六:

134823|saddle midnight thistle honeydew lime|Manufacturer#4|Brand#43|STANDARD BURNISHED BRASS|44|WRAP CAN|1857.82|ges. furiously ir|
134824|coral red indian thistle sandy|Manufacturer#5|Brand#55|PROMO BURNISHED COPPER|29|LG JAR|1858.82|final p|
134825|saddle purple orchid cornsilk medium|Manufacturer#4|Brand#44|PROMO POLISHED NICKEL|21|LG CASE|1859.82|nal accounts us|
134826|turquoise sky lime cornsilk peach|Manufacturer#1|Brand#11|SMALL BURNISHED TIN|25|SM CAN|1860.82| haggle|
第七:

0|AFRICA|lar deposits. blithely final packages cajole. regular waters are final requests. regular accounts are according to |
1|AMERICA|hs use ironic, even requests. s|
第八:

4|136777|O|32151.78|1995-10-11|5-LOW|Clerk#000000124|0|sits. slyly regular warthogs cajole. regular, regular theodolites acro|
5|44485|F|144659.20|1994-07-30|5-LOW|Clerk#000000925|0|quickly. bold deposits sleep slyly. packages use slyly|
(如果您不知道,csv是使用TPC fpr TPC-h中的DBGen工具创建的)

我希望你理解我的需要,并能在这方面帮助我。非常感谢

编辑:使用String.split(“|”)进行编辑59

EDIT2:问题是我正在尝试使用OLTP测试台上的TPC-H实现:


其中有问题的行是347。这是TPC-H数据库基准的完整实现,但没有数据生成器。因此我使用TPC提供的dbgen工具生成csv文件。遗憾的是,我无法与开发人员联系。

我建议使用
String.split(“|”);
。这将为您提供一个字符串数组,表示行周围的文本

但是,如果确实要使用正则表达式:

Pattern csvPattern = Pattern.compile("\\s*(\\d*)\\|(([^|]+)\\|)+");

这一个应该匹配一个数字(或无)、一个管道,然后是一个重复的
Something |

模式。根据您的源代码,您可能只需将逗号替换为管道,因为从注释中,该模式所做的只是在分隔符上拆分字符串(双引号中的除外)

例如:从

\\s*(\"[^\"]*\"|[^,]*)\\s*,?

至于数字异常,您需要调试调用CSV加载程序的方式

我以前从未使用过这个工具,但如果你看第352行

for (int i = 0; i < types.length; ++i) {

如果您没有正确指定类型,这种类型的转换可能会导致问题。

使用regex而不是
String.split()有什么原因吗
?您不使用CSV解析器有什么原因吗?您能解释一下拆分的哪一部分需要正则表达式吗?例如,一些工具在没有正确转义值的情况下导出CSV文件是很常见的,然后您就会陷入混乱,无法直接拆分分隔符。不,对不起。我不能,因为我真的不知道是什么我正在做:(我更新了postthaks以获取您的建议!遗憾的是,它对我不起作用。抱歉,我不够清楚。我编辑了我的原始帖子。哇,这些管道之间有很多可能发生的事情。让我再看看这是一个正确的想法,但有两个警告:第一,MxyL取消了字符串中的一些转义…第二,一个错误。)o-width字符串将与此表达式匹配。由于所有示例都以管道结尾,因此我将删除最后的
。最终答案:
\\s*(\“[^\”]*\“[^]*)\\我把转义码放了回去,但我认为这会降低可读性。这是用Java做转义码的一个困难…如果你把转义码从第一行FWIW中去掉,我会很好。我只是觉得你似乎建议将转义版本改成非转义版本让人困惑。@user3332816你的issue不再与正则表达式相关。你需要调试你的代码。@dcsohl我明白你的意思。是的,一致性会令人困惑。
for (int i = 0; i < types.length; ++i) {
switch(types[i]) {
    case DOUBLE:
        prepStmt.setDouble(i+1, Double.parseDouble(field));
        break;
...