简体   繁体   English

Java 简单行解析器

[英]Java simple line parser

I could see bunch of java parsers like OpenCSV, antlr, jsapar etc, but I dont see any of those with ability to specify both custom line seperator and column seperator?我可以看到一堆 Java 解析器,如 OpenCSV、antlr、jsapar 等,但我没有看到任何能够同时指定自定义行分隔符和列分隔符的解析器? Do we have any such easy to use libraries.我们有这样易于使用的库吗? I dont want to write one using Scanner or Stringtokenizer now!我现在不想使用 Scanner 或 Stringtokenizer 编写一个!

Eg.例如。 A |一个 | B ||乙|| C |丙 | D || D || E |乙 | F F

want to break this above string to something like {{A,B},{C,D},{E,F}}想将上面的字符串分解为 {{A,B},{C,D},{E,F}}

You can parse it yourself, it's quite simple to achieve.大家可以自己解析,实现起来还是挺简单的。 I haven't test this code practically, you may try it yourself.我没有实际测试过这段代码,你可以自己试试。

line_delimiter = "||";
column_delimiter = "|";

String rows[];
rows = str.split(line_delimiter);
for (int i = 0; i < rows.length; i++) {
    String columns[];
    columns = rows[i].split(column_delimiter);
    for (int j = 0; j < columns.length; j++) {
        // Do something to your data here;
    }
}

Actually, with JSaPar you can have any character sequence for both line delimiter as well as cell delimiter.实际上,使用JSaPar ,您可以为行定界符和单元格定界符使用任何字符序列。 You specify which delimiter to use within your schema and it can be any number of characters.您指定要在架构中使用的分隔符,它可以是任意数量的字符。

The problem you will face by using the same character in both is that the parser will not know if you have a line break or if it is just an empty cell.如果在两者中使用相同的字符,您将面临的问题是解析器将不知道您是否有换行符或者它是否只是一个空单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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