【问题标题】:regular expression on a csv file in JavaJava中csv文件的正则表达式
【发布时间】:2017-05-02 22:31:23
【问题描述】:

我必须从 CSV 文件中找出符合特定搜索条件的行。 CSV 文件中的数据如下所示:

Wilbur Smith,Elephant Song,McMillain,1992,1
Wilbur Smith,Birds of Prey,McMillain,1992,1
George Orwell,Animal Farm,Secker & Warburg,1945,1
George Orwell,1984,Secker & Warburg,1949,1

搜索条件是这样的:

Orwell,,,,
,Elephant,,,

第一行标识 2 行,第二行标识 1 行。我目前正在按如下方式读取文件,但未使用上述标准。

br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
    String[] dataItems = line.split(cvsSplitBy);

    if (dataItems[0].contains(title) && dataItems[1].contains(author) && dataItems[2].contains(publisher)) {
        bk[i++] = line;
        if (bk.length > 4) {break;}
    }
}

我正在添加一个固定大小的数组。如何使用条件作为正则表达式来识别行?

【问题讨论】:

  • 您应该使用 CSV 解析器,以便它可以处理嵌入的逗号。然后你应该简单地使用contains() 来查看列值是否包含给定的文本,就像你现在正在做的那样。不需要正则表达式。
  • 我不是 Java 专家。您会对仅使用正则表达式的答案感到满意吗?还是你也需要java代码来使用它?
  • 你认为你为什么需要正则表达式?
  • 大部分子字符串为空。因此,例如,如果发布者为空,则不应发生对发布者的搜索。我不是 Java 专家,但正则表达式不会比使用 contains 更好吗?包含 | ..等
  • 您应该只将这些数据提取到类中,然后使用.contains() 或类似方法进行搜索,而不是尝试使用正则表达式。这个问题不适合使用正则表达式 IMO。

标签: java regex csv


【解决方案1】:

似乎我在这里是少数 :) 但如果您有兴趣,这里有一个使用正则表达式的版本。

BufferedReader br = null;

String[] searches = new String[]{
            ",Animal Farm,Secker & Warburg,,",
            ",,Secker & Warburg,,",
            "George Orwell,,,,1",
            "Wilbur Smith,,,,",
            ",,,,1",
            "random,,,,1",
            "WILBUR SMITH,Birds of PREY,mcmillain,1992,1",
            ",,,,"
};

try {
    br = new BufferedReader(new FileReader("file.txt"));
    String line = null;

    // to store results of matches for easier output
    String[] matchResult = new String[searches.length];

    while ((line = br.readLine()) != null) {
        // go through all searches
        for (int i = 0; i < searches.length; i++) {

            /*
             *  replace all commas that don't have letters or numbers on both 
             *  sides with a new regex to match all characters
             */
            String searchPattern = searches[i].replaceAll("(?<![a-zA-z0-9])\\,|\\,(?![a-zA-z0-9\\,])", ".*,.*");

            // do the match on the line
            Matcher m = Pattern.compile("^" + searchPattern + "$", Pattern.CASE_INSENSITIVE).matcher(line);

            // store the result
            matchResult[i] = m.matches() == true ? "matches" : "no match";
        }

        System.out.println(String.format("%-50s %-10s %-10s %-10s %-10s %-10s %-10s %-10s", line, 
                    matchResult[0], matchResult[1], matchResult[2], matchResult[3], matchResult[4], matchResult[5], matchResult[6], matchResult[7]));
    }
} catch (Exception e) {
        e.printStackTrace();
} finally {
    try {
        br.close();
    } catch (IOException e) {}
}

输出

Wilbur Smith,Elephant Song,McMillain,1992,1        no match   no match   no match   matches    matches    no match   no match  
Wilbur Smith,Birds of Prey,McMillain,1992,1        no match   no match   no match   matches    matches    no match   matches   
George Orwell,Animal Farm,Secker & Warburg,1945,1  matches    matches    matches    no match   matches    no match   no match  
George Orwell,1984,Secker & Warburg,1949,1         no match   matches    matches    no match   matches    no match   no match 

【讨论】:

  • 谢谢,虽然我很欣赏其他人所说的性能和复杂性,但我只是想看看硬币的另一面,也就是正则表达式的用法。
猜你喜欢
  • 2012-11-10
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 2011-03-17
  • 2015-12-29
  • 1970-01-01
相关资源
最近更新 更多